Completed
Pull Request — develop (#109)
by Alexander
13:01
created

ArticleRouteDefaultProvider::getByEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 9
nc 2
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Routing;
13
14
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
15
use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface;
16
use Sulu\Component\Content\Document\WorkflowStage;
17
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
18
use Sulu\Component\Content\Metadata\StructureMetadata;
19
use Sulu\Component\DocumentManager\DocumentManagerInterface;
20
use Sulu\Component\HttpCache\CacheLifetimeResolverInterface;
21
22
/**
23
 * Provides route-defaults for articles.
24
 */
25
class ArticleRouteDefaultProvider implements RouteDefaultsProviderInterface
26
{
27
    /**
28
     * @var DocumentManagerInterface
29
     */
30
    private $documentManager;
31
32
    /**
33
     * @var StructureMetadataFactoryInterface
34
     */
35
    private $structureMetadataFactory;
36
37
    /**
38
     * @var CacheLifetimeResolverInterface
39 4
     */
40
    private $cacheLifetimeResolver;
41
42
    /**
43 4
     * @param DocumentManagerInterface $documentManager
44 4
     * @param StructureMetadataFactoryInterface $structureMetadataFactory
45 4
     */
46
    public function __construct(
47
        DocumentManagerInterface $documentManager,
48
        StructureMetadataFactoryInterface $structureMetadataFactory,
49
        CacheLifetimeResolverInterface $cacheLifetimeResolver
50
    ) {
51
        $this->documentManager = $documentManager;
52 1
        $this->structureMetadataFactory = $structureMetadataFactory;
53
        $this->cacheLifetimeResolver = $cacheLifetimeResolver;
54 1
    }
55 1
56
    /**
57
     * {@inheritdoc}
58 1
     *
59
     * @param ArticleDocument $object
60
     */
61 1
    public function getByEntity($entityClass, $id, $locale, $object = null)
62 1
    {
63 1
        if (!$object) {
64 1
            $object = $this->documentManager->find($id, $locale);
65
        }
66
67
        $metadata = $this->structureMetadataFactory->getStructureMetadata('article', $object->getStructureType());
68
69
        return [
70
            'object' => $object,
71
            'view' => $metadata->view,
72
            '_cacheLifetime' => $this->getCacheLifetime($metadata),
73
            '_controller' => $metadata->controller,
74 3
        ];
75
    }
76 3
77
    /**
78 3
     * If article is not published the document will be of typ unknown-document.
79
     * Also check the workflow stage if it`s a ArticleDocument.
80
     *
81
     * {@inheritdoc}
82
     */
83
    public function isPublished($entityClass, $id, $locale)
84
    {
85
        $object = $this->documentManager->find($id, $locale);
86
87
        return $object instanceof ArticleDocument && WorkflowStage::PUBLISHED === $object->getWorkflowStage();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function supports($entityClass)
94
    {
95
        return $entityClass === ArticleDocument::class;
96
    }
97
98
    /**
99
     * Get cache life time.
100
     *
101
     * @param StructureMetadata $metadata
102
     *
103
     * @return int|null
104
     */
105
    private function getCacheLifetime($metadata)
106
    {
107
        $cacheLifetime = $metadata->cacheLifetime;
108
109
        if (!$cacheLifetime) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cacheLifetime of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110
            return null;
111
        }
112
113
        if (!is_array($cacheLifetime) || !isset($cacheLifetime['type']) || !isset($cacheLifetime['value'])) {
114
            throw new \InvalidArgumentException(
115
                sprintf('Invalid cachelifetime in article route default provider: %s', var_export($cacheLifetime, true))
116
            );
117
        }
118
119
        $this->cacheLifetimeResolver->supports($cacheLifetime['type'], $cacheLifetime['value']);
120
121
        return $this->cacheLifetimeResolver->resolve($cacheLifetime['type'], $cacheLifetime['value']);
122
    }
123
}
124