From 16c5c92415cdf70a1cf7a06ef8af446ba13114ea Mon Sep 17 00:00:00 2001 From: dantleech Date: Tue, 24 May 2016 14:40:05 +0100 Subject: [PATCH] Refactored --- Description/Description.php | 62 ++++++++++++ Description/DescriptionEnricherInterface.php | 27 +++++ Description/DescriptionFactory.php | 49 +++++++++ Description/Descriptor.php | 48 +++++++++ Metadata/PayloadMetadata.php | 68 ------------- Metadata/PayloadMetadataFactoryInterface.php | 22 ----- Metadata/SonataMetadataFactory.php | 92 ----------------- Metadata/SyliusMetadataFactory.php | 77 --------------- .../Description/DescriptionFactoryTest.php | 99 +++++++++++++++++++ Tests/Unit/Description/DescriptionTest.php | 54 ++++++++++ 10 files changed, 339 insertions(+), 259 deletions(-) create mode 100644 Description/Description.php create mode 100644 Description/DescriptionEnricherInterface.php create mode 100644 Description/DescriptionFactory.php create mode 100644 Description/Descriptor.php delete mode 100644 Metadata/PayloadMetadata.php delete mode 100644 Metadata/PayloadMetadataFactoryInterface.php delete mode 100644 Metadata/SonataMetadataFactory.php delete mode 100644 Metadata/SyliusMetadataFactory.php create mode 100644 Tests/Unit/Description/DescriptionFactoryTest.php create mode 100644 Tests/Unit/Description/DescriptionTest.php diff --git a/Description/Description.php b/Description/Description.php new file mode 100644 index 0000000..a33b611 --- /dev/null +++ b/Description/Description.php @@ -0,0 +1,62 @@ +payloadType = $payloadType; + } + + /** + * Return the descriptors value for the given key. + * + * The key should be one of the constants defined in this class. + * + * @param string $key + * @return mixed + */ + public function get($key) + { + if (!isset($this->descriptors[$key])) { + throw new \InvalidArgumentException(sprintf( + 'Descriptor "%s" not supported for payload type "%s". Supported descriptors: "%s"', + $key, + $this->payloadType, + implode('", "', array_keys($this->descriptors)) + )); + } + + return $this->descriptors[$key]; + } + + /** + * Set value for descriptors key. + * + * - It is possible to overwrite existing keys. + * + * - To help ensure interoperability, where possible, the key should be the + * value of one of the appropriate constants defined in the MetadescriptorsKey + * class. + * + * @param string $key + * @param mixed $value + */ + public function set($key, $value) + { + $this->descriptors[$key] = $value; + } +} diff --git a/Description/DescriptionEnricherInterface.php b/Description/DescriptionEnricherInterface.php new file mode 100644 index 0000000..79994b5 --- /dev/null +++ b/Description/DescriptionEnricherInterface.php @@ -0,0 +1,27 @@ +enrichers = $enrichers; + } + + /** + * Return a description of the given (CMF) Resource. + * + * @param CmfResource $resource + */ + public function getPayloadDescriptionFor(CmfResource $resource) + { + $type = $resource->getPayloadType(); + $payload = $resource->getPayload(); + $description = new Description($type); + + foreach ($this->enrichers as $enricher) { + if (false === $enricher->supports($resource)) { + continue; + } + + $enricher->enrich($description, $payload); + } + + return $description; + } +} diff --git a/Description/Descriptor.php b/Description/Descriptor.php new file mode 100644 index 0000000..f68372c --- /dev/null +++ b/Description/Descriptor.php @@ -0,0 +1,48 @@ +providerName = $providerName; - $this->metadata = $metadata; - } - - /** - * Return the metadata value for the given key. - * - * The key should be one of the constants defined in this class. - * - * @param string $key - * @return mixed - */ - public function get($key) - { - if (!isset($this->supported[$key])) { - throw new \InvalidArgumentException(sprintf( - 'Metadata key "%s" from "%s" not supported', - $key, - $this->providerName - )); - } - - return $this->metadata[$key]; - } -} diff --git a/Metadata/PayloadMetadataFactoryInterface.php b/Metadata/PayloadMetadataFactoryInterface.php deleted file mode 100644 index 33aeffe..0000000 --- a/Metadata/PayloadMetadataFactoryInterface.php +++ /dev/null @@ -1,22 +0,0 @@ - - */ -class SonataMetadataFactory implements PayloadMetadataFactoryInterface -{ - /** - * @var Pool - */ - private $pool; - - /** - * @var UrlGeneratorInterface - */ - private $urlGenerator; - - /** - * __construct - * - * @param Pool $pool - * @param UrlGeneratorInterface $urlGenerator - */ - public function __construct(Pool $pool, UrlGeneratorInterface $urlGenerator) - { - $this->pool = $pool; - $this->urlGenerator = $urlGenerator; - } - - /** - * {@inheritDoc} - */ - public function get(CmfResource $resource) - { - $object = $resource->getPayload(); - - // sonata has dependency on ClassUtils so this is fine. - $class = ClassUtils::getClass($object); - - if (false === $this->pool->hasAdminByClass($class)) { - return $data; - } - - $admin = $this->pool->getAdminByClass($class); - - $links = array(); - - $routeCollection = $admin->getRoutes(); - - foreach ($routeCollection->getElements() as $code => $route) { - $routeName = $route->getDefault('_sonata_name'); - $url = $this->urlGenerator->generate($routeName, array( - $admin->getIdParameter() => $admin->getUrlsafeIdentifier($object), - ), true); - - $routeRole = substr($code, strlen($admin->getCode()) + 1); - - $links[$routeRole] = $url; - } - - $metadata = new PayloadMetadata( - 'sonata', - [ - PayloadMetadata::TITLE => $admin->toString($object), - PayloadMetadata::TYPE_TITLE => $admin->getLabel(), - PayloadMetadata::LINK_EDIT_HTML => $links['edit'], - PayloadMetadata::LINK_UPDATE_HTML => $links['update'], - PayloadMetadata::LINK_CREATE_HTML => $links['create'], - PayloadMetadata::LINK_REMOVE_HTML => $links['remove'], - ] - ); - - return $metadata; - } -} diff --git a/Metadata/SyliusMetadataFactory.php b/Metadata/SyliusMetadataFactory.php deleted file mode 100644 index 64933e0..0000000 --- a/Metadata/SyliusMetadataFactory.php +++ /dev/null @@ -1,77 +0,0 @@ - - */ -class SyliusMetadataFactory implements PayloadMetadataFactoryInterface -{ - /** - * @var Registry - */ - private $registry; - - /** - * @var UrlGeneratorInterface - */ - private $urlGenerator; - - /** - * __construct - * - * @param Registry $registry - * @param UrlGeneratorInterface $urlGenerator - */ - public function __construct(Registry $registry, UrlGeneratorInterface $urlGenerator) - { - $this->registry = $registry; - $this->urlGenerator = $urlGenerator; - } - - /** - * {@inheritDoc} - */ - public function get(CmfResource $resource) - { - $object = $resource->getPayload(); - - // sonata has dependency on ClassUtils so this is fine. - $class = ClassUtils::getClass($object); - - // TODO: does this already throw an exception? - $metadata = $this->registry->getMetadataForClass($class); - - $metadata = new PayloadMetadata( - 'sonata', - [ - PayloadMetadata::ALIAS => $metadata->getAlias(), - // PayloadMetadata::TITLE => TODO: Is this supported? - PayloadMetadata::TYPE_TITLE => $metadata->getAlias(), // maybe run this through the translator - - // TODO: Links are by convention - PayloadMetadata::LINK_EDIT_HTML => null, - PayloadMetadata::LINK_UPDATE_HTML => null, - PayloadMetadata::LINK_CREATE_HTML => null, - PayloadMetadata::LINK_REMOVE_HTML => null, - ] - ); - - return $metadata; - } -} diff --git a/Tests/Unit/Description/DescriptionFactoryTest.php b/Tests/Unit/Description/DescriptionFactoryTest.php new file mode 100644 index 0000000..401f488 --- /dev/null +++ b/Tests/Unit/Description/DescriptionFactoryTest.php @@ -0,0 +1,99 @@ +enricher1 = $this->prophesize(DescriptionEnricherInterface::class); + $this->enricher2 = $this->prophesize(DescriptionEnricherInterface::class); + $this->resource = $this->prophesize(CmfResource::class); + + $this->payload = new \stdClass(); + $this->resource->getPayload()->willReturn($this->payload); + $this->resource->getPayloadType()->willReturn('payload-type'); + } + + /** + * It should return an enriched description. + */ + public function testGetPayloadDescription() + { + $this->enricher1->enrich(Argument::type(Description::class), $this->payload) + ->will(function ($args) { + $description = $args[0]; + $description->set('foobar', 'barfoo'); + }); + $this->enricher1->supports($this->resource->reveal())->willReturn(true); + $this->enricher2->enrich(Argument::type(Description::class), $this->payload) + ->will(function ($args) { + $description = $args[0]; + $description->set('barfoo', 'foobar'); + }); + $this->enricher2->supports($this->resource->reveal())->willReturn(true); + + $description = $this->createFactory([ + $this->enricher1->reveal(), + $this->enricher2->reveal() + ])->getPayloadDescriptionFor($this->resource->reveal()); + + $this->assertInstanceOf(Description::class, $description); + $this->assertEquals('barfoo', $description->get('foobar')); + $this->assertEquals('foobar', $description->get('barfoo')); + } + + /** + * It should ignore providers that do not support the payload type. + */ + public function testIgnoreNonSupporters() + { + $this->enricher1->enrich(Argument::cetera())->shouldNotBeCalled(); + $this->enricher1->supports($this->resource->reveal())->willReturn(false); + + $this->enricher2->enrich(Argument::cetera())->shouldBeCalled(); + $this->enricher2->supports($this->resource->reveal())->willReturn(true); + + $this->createFactory([ + $this->enricher1->reveal(), + $this->enricher2->reveal() + ])->getPayloadDescriptionFor($this->resource->reveal()); + } + + /** + * It should work when no enrichers are given. + */ + public function testNoEnrichers() + { + $description = $this->createFactory([])->getPayloadDescriptionFor($this->resource->reveal()); + $this->assertInstanceOf(Description::class, $description); + } + + private function createFactory(array $enrichers) + { + return new DescriptionFactory($enrichers); + } +} diff --git a/Tests/Unit/Description/DescriptionTest.php b/Tests/Unit/Description/DescriptionTest.php new file mode 100644 index 0000000..8185380 --- /dev/null +++ b/Tests/Unit/Description/DescriptionTest.php @@ -0,0 +1,54 @@ +description = new Description('some-type'); + } + + /** + * It should allow values to be set and retrieved. + */ + public function testGetSet() + { + $this->description->set(Descriptor::TYPE_ALIAS, 'page'); + $this->description->set(Descriptor::LINK_EDIT_HTML, '/path/to/edit'); + $this->description->set('custom.key', 'Hello'); + + $this->assertEquals('page', $this->description->get(Descriptor::TYPE_ALIAS)); + } + + /** + * It should throw an exception when requesting an unsupported descriptor. + * + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Descriptor "not there" not supported for payload type "some-type". Supported descriptors: "foo", "bar" + */ + public function testGetUnsupported() + { + $this->description->set('foo', 'bar'); + $this->description->set('bar', 'foo'); + $this->description->get('not there'); + } +}