Skip to content

Commit

Permalink
Merge pull request #1 from ShopwareAcademy/adhoc/change-api-to-be-mor…
Browse files Browse the repository at this point in the history
…e-accessible

Change example
  • Loading branch information
Isengo1989 authored Nov 26, 2024
2 parents fae2fad + 996bd74 commit 98458ad
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# StorefrontControllerPlugin
# AcademyStorefrontController

A plugin which shows how to create a simple Storefront controller

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "academy/storefront-controller",
"name": "shopware-academy/storefront-controller",
"description": "Storefront plugin with custom functionality",
"type": "shopware-platform-plugin",
"version": "1.0.0",
Expand All @@ -8,20 +8,20 @@
"shopware/core": "~6.6.0"
},
"extra": {
"shopware-plugin-class": "StorefrontControllerPlugin\\StorefrontControllerPlugin",
"shopware-plugin-class": "ShopwareAcademy\\StorefrontController\\AcademyStorefrontController",
"label": {
"de-DE": "Plugin mit eigenem Storefront Controller",
"en-GB": "Plugin with custom storefront controller"
}
},
"autoload": {
"psr-4": {
"StorefrontControllerPlugin\\": "src/"
"ShopwareAcademy\\StorefrontController\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"StorefrontControllerPlugin\\Tests\\": "tests/"
"ShopwareAcademy\\StorefrontController\\Tests\\": "tests/"
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
</php>
<testsuites>
<testsuite name="StorefrontControllerPlugin Testsuite">
<testsuite name="AcademyStorefrontController Testsuite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace StorefrontControllerPlugin;
namespace ShopwareAcademy\StorefrontController;

use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
Expand All @@ -9,7 +9,7 @@
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;

class StorefrontControllerPlugin extends Plugin
class AcademyStorefrontController extends Plugin
{
public function install(InstallContext $installContext): void
{
Expand Down
20 changes: 18 additions & 2 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/System/SystemConfig/Schema/config.xsd">

<card>
<title>Unsplash access key</title>
<title>API settings</title>
<input-field type="single-select">
<name>apiProvider</name>
<label>Are you a cat or dog person?</label>
<options>
<option>
<id>thecatapi</id>
<name>I am a cat person</name>
</option>
<option>
<id>thedogapi</id>
<name>I am a dog person</name>
</option>
</options>
<defaultValue>thedogapi</defaultValue>
</input-field>

<input-field type="password">
<name>unsplashAccessKey</name>
<name>apiAccessKey</name>
<label>Access Key</label>
<helpText>You don't need an API key to get a single image. This input is optional for > 10 images. https://developers.thecatapi.com</helpText>
</input-field>
</card>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<services>

<service id="StorefrontControllerPlugin\Storefront\Controller\ImageController" public="true">
<service id="ShopwareAcademy\StorefrontController\Storefront\Controller\ImageController" public="true">
<argument type="service" id="shopware.app_system.guzzle"/>
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
<call method="setContainer">
Expand Down
13 changes: 7 additions & 6 deletions src/Storefront/Controller/ImageController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace StorefrontControllerPlugin\Storefront\Controller;
namespace ShopwareAcademy\StorefrontController\Storefront\Controller;

use GuzzleHttp\Client;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
Expand Down Expand Up @@ -29,18 +29,19 @@ public function __construct(Client $client, SystemConfigService $systemConfigSer
)]
public function showImage(Request $request, SalesChannelContext $context): Response
{
$unsplashAccessKey = $this->systemConfigService->get('StorefrontControllerPlugin.config.unsplashAccessKey');
$apiAccessKey = $this->systemConfigService->get('AcademyStorefrontController.config.apiAccessKey');
$apiProvider = $this->systemConfigService->get('AcademyStorefrontController.config.apiProvider');

$response = $this->client->request('GET', 'https://api.unsplash.com/photos/random', [
$response = $this->client->request('GET', 'https://api.'.$apiProvider.'.com/v1/images/search', [
'headers' => [
'Authorization' => 'Client-ID ' . $unsplashAccessKey
'x-api-key' => $apiAccessKey
]
]);

$data = json_decode($response->getBody()->getContents(), true);
$imageUrl = $data['urls']['regular'];
$imageUrl = $data[0]['url'];

return $this->renderStorefront('@StorefrontControllerPlugin/storefront/page/image.html.twig', [
return $this->renderStorefront('@AcademyStorefrontController/storefront/page/image.html.twig', [
'imageUrl' => $imageUrl
]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

$loader = (new TestBootstrapper())
->addCallingPlugin()
->addActivePlugins('StorefrontControllerPlugin')
->addActivePlugins('AcademyStorefrontController')
->setForceInstallPlugins(true)
->bootstrap()
->getClassLoader();

$loader->addPsr4('StorefrontControllerPlugin\\Tests\\', __DIR__);
$loader->addPsr4('AcademyStorefrontController\\StorefrontController\\Tests\\', __DIR__);

0 comments on commit 98458ad

Please sign in to comment.