Skip to content

Commit

Permalink
Setup Cloudinary
Browse files Browse the repository at this point in the history
  • Loading branch information
pchasle committed Sep 24, 2019
1 parent ef86376 commit 15e5427
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ AKENEO_CLIENT_ID=
AKENEO_SECRET=
AKENEO_USERNAME=
AKENEO_PASSWORD=

# Cloudinary credentials
CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
5 changes: 5 additions & 0 deletions .php_cd.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
/**
* INFRASTRUCTURE
*/
$builder->only([
'AkeneoDAMConnector\Application\DamAdapter',
'AkeneoDAMConnector\Domain',
])->in('AkeneoDAMConnector\Infrastructure\DAM\Cloudinary'),

$builder->only([
'AkeneoDAMConnector\Application\DamAdapter',
'AkeneoDAMConnector\Domain',
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"akeneo/api-php-client-ee": "dev-asset-family@dev",
"akeneo/api-php-client": "dev-master@dev",
"akeneo/api-php-client-ee": "dev-asset-family@dev",
"cloudinary/cloudinary_php": "1.14.*",
"doctrine/dbal": "2.9.*",
"http-interop/http-factory-guzzle": "^1.0",
"php-http/guzzle6-adapter": "^2.0",
Expand Down
58 changes: 55 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
20 changes: 20 additions & 0 deletions config/services/cloudinary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:
app.pim_structure.config_path: '%kernel.project_dir%/config/resources/cloudinary/structure.yaml'
app.dam_to_pim_mapping.config_path: '%kernel.project_dir%/config/resources/cloudinary/mapping.yaml'

services:
_defaults:
autowire: true
autoconfigure: true
public: false

AkeneoDAMConnector\Infrastructure\DAM\Cloudinary\:
resource: '../../src/Infrastructure/DAM/Cloudinary/*'

AkeneoDAMConnector\Infrastructure\DAM\Cloudinary\Search:
arguments:
$cloudName: '%env(CLOUDINARY_CLOUD_NAME)%'
$apiKey: '%env(CLOUDINARY_API_KEY)%'
$apiSecret: '%env(CLOUDINARY_API_SECRET)%'

AkeneoDAMConnector\Application\DamAdapter\FetchAssets: '@AkeneoDAMConnector\Infrastructure\DAM\Cloudinary\FetchAssets'
File renamed without changes.
55 changes: 55 additions & 0 deletions src/Infrastructure/DAM/Cloudinary/FetchAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace AkeneoDAMConnector\Infrastructure\DAM\Cloudinary;

use AkeneoDAMConnector\Application\DamAdapter\FetchAssets as FetchAssetsInterface;
use AkeneoDAMConnector\Domain\Asset\DamAsset;
use AkeneoDAMConnector\Domain\Asset\DamAssetCollection;
use AkeneoDAMConnector\Domain\Asset\DamAssetIdentifier;
use AkeneoDAMConnector\Domain\AssetFamilyCode;
use AkeneoDAMConnector\Domain\Locale;
use AkeneoDAMConnector\Domain\ResourceType;

class FetchAssets implements FetchAssetsInterface
{
/** @var Search */
private $search;

public function __construct(Search $search)
{
$this->search = $search;
}

public function fetch(\DateTime $lastFetchDate, AssetFamilyCode $assetFamilyCode): DamAssetCollection
{
$expression = sprintf('tags:akeneo AND folder="%s"', (string) $assetFamilyCode);
$response = $this->search->search($expression, ['tags', 'context']);

$staticAttributes = ['filename', 'url', 'secure_url', 'status', 'public_id'];
$damAssets = new DamAssetCollection();
$assets = $response['resources'] ?? [];
foreach ($assets as $asset) {
$damAsset = new DamAsset(
new DamAssetIdentifier($asset['filename']),
$assetFamilyCode,
new Locale('en_US'),
new ResourceType($asset['resource_type'])
);

foreach ($staticAttributes as $staticAttribute) {
$damAsset->addValue($staticAttribute, $asset[$staticAttribute]);
}

foreach ($asset['context'] as $property => $value) {
$damAsset->addValue($property, $value);
}
$damAsset->addValue('tags', implode(', ', $asset['tags']));

$damAssets->addAsset($damAsset);
}

return $damAssets;
}
}
31 changes: 31 additions & 0 deletions src/Infrastructure/DAM/Cloudinary/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace AkeneoDAMConnector\Infrastructure\DAM\Cloudinary;

class Search
{
public function __construct(string $cloudName, string $apiKey, string $apiSecret)
{
\Cloudinary::config(array(
'cloud_name' => $cloudName,
'api_key' => $apiKey,
'api_secret' => $apiSecret,
'secure' => true
));
}

public function search(string $expression, array $withFields)
{
$searchEngine = new \Cloudinary\Search();
$searchEngine->expression($expression);
if (!empty($withFields)) {
foreach ($withFields as $field) {
$searchEngine->with_field($field);
}
}

return $searchEngine->execute();
}
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"akeneo/php-coupling-detector": {
"version": "0.3.1"
},
"cloudinary/cloudinary_php": {
"version": "1.14.0"
},
"composer/semver": {
"version": "1.x-dev"
},
Expand Down

0 comments on commit 15e5427

Please sign in to comment.