diff --git a/lib/Alchemy/Phrasea/Border/Manager.php b/lib/Alchemy/Phrasea/Border/Manager.php index 7541be9f27..3f0adac1f2 100644 --- a/lib/Alchemy/Phrasea/Border/Manager.php +++ b/lib/Alchemy/Phrasea/Border/Manager.php @@ -13,7 +13,7 @@ namespace Alchemy\Phrasea\Border; use Alchemy\Phrasea\Border\Checker\CheckerInterface; use Alchemy\Phrasea\Border\Attribute\AttributeInterface; -use Alchemy\Phrasea\Media\Subdef\OptionType\Boolean; +use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Metadata\Tag\TfArchivedate; use Alchemy\Phrasea\Metadata\Tag\TfQuarantine; use Alchemy\Phrasea\Metadata\Tag\TfBasename; @@ -143,7 +143,7 @@ class Manager */ public function registerChecker(CheckerInterface $checker) { - $this->checkers[] = $checker; + $this->checkers[get_class($checker)] = $checker; return $this; } @@ -183,6 +183,22 @@ class Manager return $this; } + /** + * Get checker instance from its class name. + * + * @param string $checkerName + * @return CheckerInterface + */ + public function getCheckerFromFQCN($checkerName) + { + $checkerName = trim($checkerName, '\\'); + if (!isset($this->checkers[$checkerName])) { + throw new RuntimeException('Checker could not be found'); + } + + return $this->checkers[$checkerName]; + } + /** * Returns all the checkers registered * @@ -190,7 +206,7 @@ class Manager */ public function getCheckers() { - return $this->checkers; + return array_values($this->checkers); } /** diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1.php b/lib/Alchemy/Phrasea/Controller/Api/V1.php index 6d11060db7..e4ee6e2024 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1.php @@ -16,13 +16,13 @@ use Alchemy\Phrasea\Border\Attribute\Status; use Alchemy\Phrasea\Border\Checker\CheckerInterface; use Alchemy\Phrasea\Border\File; use Alchemy\Phrasea\Border\Manager as BorderManager; +use Alchemy\Phrasea\Border\Manager; use Alchemy\Phrasea\Cache\Cache as CacheInterface; use Alchemy\Phrasea\Core\Event\ApiOAuth2EndEvent; use Alchemy\Phrasea\Core\Event\ApiOAuth2StartEvent; use Alchemy\Phrasea\Core\Event\PreAuthenticate; use Alchemy\Phrasea\Core\Event\RecordEdit; use Alchemy\Phrasea\Core\PhraseaEvents; -use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Feed\Aggregate; use Alchemy\Phrasea\Feed\FeedInterface; use Alchemy\Phrasea\Model\Entities\Basket; @@ -812,20 +812,14 @@ class V1 implements ControllerProviderInterface private function list_lazaret_file(Application $app, LazaretFile $file) { - /** @var CheckerInterface[] $checkers */ - $checkers = $app['border-manager']->getCheckers(); + /** @var Manager $manager */ + $manager = $app['border-manager']; /** @var TranslatorInterface $translator */ $translator = $app['translator']; - $checks = array_map(function (LazaretCheck $checker) use ($checkers, $translator) { + $checks = array_map(function (LazaretCheck $checker) use ($manager, $translator) { $checkerFQCN = $checker->getCheckClassname(); - foreach ($checkers as $actualChecker) { - if (get_class($actualChecker) === $checkerFQCN) { - return $actualChecker->getMessage($translator); - } - } - - throw new RuntimeException('Could not find checker'); + return $manager->getCheckerFromFQCN($checkerFQCN)->getMessage($translator); }, iterator_to_array($file->getChecks())); $usr_id = $user = null; diff --git a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php index d42c14cb76..2f210b920a 100644 --- a/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php +++ b/lib/Alchemy/Phrasea/Twig/PhraseanetExtension.php @@ -5,14 +5,15 @@ namespace Alchemy\Phrasea\Twig; use Alchemy\Phrasea\Model\Entities\ElasticsearchRecord; use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\RecordInterface; +use Silex\Application; class PhraseanetExtension extends \Twig_Extension { + /** @var Application */ private $app; - public function __construct( - $app - ) { + public function __construct(Application $app) + { $this->app = $app; } @@ -37,7 +38,8 @@ class PhraseanetExtension extends \Twig_Extension new \Twig_SimpleFunction('collection_logo', array($this, 'getCollectionLogo'), array( 'is_safe' => array('html') )), - new \Twig_SimpleFunction('record_flags', array($this, 'getRecordFlags')) + new \Twig_SimpleFunction('record_flags', array($this, 'getRecordFlags')), + new \Twig_SimpleFunction('border_checker_from_fqcn', array($this->app['border-manager'], 'getCheckerFromFQCN')), ); } diff --git a/lib/classes/eventsmanager/notify/uploadquarantine.php b/lib/classes/eventsmanager/notify/uploadquarantine.php index 2c89ffc30d..9aff705218 100644 --- a/lib/classes/eventsmanager/notify/uploadquarantine.php +++ b/lib/classes/eventsmanager/notify/uploadquarantine.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -use Alchemy\Phrasea\Border\Checker\CheckerInterface; +use Alchemy\Phrasea\Border\Manager; use Alchemy\Phrasea\Model\Entities\User; use Symfony\Component\Translation\TranslatorInterface; @@ -32,19 +32,13 @@ class eventsmanager_notify_uploadquarantine extends eventsmanager_notifyAbstract */ public function datas(array $data, $unread) { - /** @var CheckerInterface[] $checkers */ - $checkers = $this->app['border-manager']->getCheckers(); + /** @var Manager $manager */ + $manager = $this->app['border-manager']; /** @var TranslatorInterface $translator */ $translator = $this->app['translator']; - $reasons = array_map(function ($checkerFQCN) use ($checkers, $translator) { - foreach ($checkers as $actualChecker) { - if (get_class($actualChecker) === $checkerFQCN) { - return $actualChecker->getMessage($translator); - } - } - - throw new RuntimeException('Could not find checker'); + $reasons = array_map(function ($checkerFQCN) use ($manager, $translator) { + return $manager->getCheckerFromFQCN($checkerFQCN)->getMessage($translator); }, $data['reasons']); $filename = $data['filename']; diff --git a/templates/web/prod/upload/lazaret.html.twig b/templates/web/prod/upload/lazaret.html.twig index 26114a863c..5f9ce13800 100644 --- a/templates/web/prod/upload/lazaret.html.twig +++ b/templates/web/prod/upload/lazaret.html.twig @@ -334,7 +334,7 @@
{{ check.getMessage(app['translator']) }}
+{{ border_checker_from_fqcn(check.getCheckClassname()).getMessage(app['translator']) }}
{% endfor %}