Skip to content

Commit

Permalink
Setup Cloudinary
Browse files Browse the repository at this point in the history
  • Loading branch information
pchasle authored and fitn committed Oct 4, 2019
1 parent 780bc02 commit 536468e
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 5 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.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

imports:
# Import the configuration for our DAM example implementation
- { resource: services/dam-example.yaml }
- { resource: services/dam-adapter.yaml }

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
Expand Down
20 changes: 20 additions & 0 deletions config/services/dam-adapter.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.
60 changes: 60 additions & 0 deletions src/Infrastructure/DAM/Cloudinary/FetchAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace AkeneoDAMConnector\Infrastructure\DAM\Cloudinary;

use AkeneoDAMConnector\Application\DamAdapter\FetchAssets as FetchAssetsInterface;
use AkeneoDAMConnector\Domain\Model\Dam\DamAsset;
use AkeneoDAMConnector\Domain\Model\Dam\DamAssetIdentifier;
use AkeneoDAMConnector\Domain\Model\FamilyCode;
use AkeneoDAMConnector\Domain\Model\Locale;

/**
* This class is not scalable for an high volume of assets
*
* Cloudinary does not provide any delta to fetch assets from a last update date
* They use a queuing system for that
*/
class FetchAssets implements FetchAssetsInterface
{
/** @var Search */
private $search;

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

public function fetch(FamilyCode $assetFamilyCode, ?\DateTimeInterface $lastFetchDate): \Iterator
{
$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 = [];

$assets = $response['resources'] ?? [];
foreach ($assets as $asset) {
$damAsset = new DamAsset(
new DamAssetIdentifier($asset['filename']),
$assetFamilyCode,
new Locale('en_US')
);

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[] = $damAsset;
}

return new \ArrayIterator($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 536468e

Please sign in to comment.