Completed
Pull Request — develop (#161)
by Wachter
22:54 queued 09:56
created

ArticleRouteDefaultProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 86.49%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 123
ccs 32
cts 37
cp 0.8649
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B getByEntity() 0 28 3
A isPublished() 0 6 2
A supports() 0 4 2
B getCacheLifetime() 0 20 6
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\ArticleBundle\Document\ArticleInterface;
16
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
17
use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface;
18
use Sulu\Component\Content\Compat\StructureManagerInterface;
19
use Sulu\Component\Content\Document\WorkflowStage;
20
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
21
use Sulu\Component\Content\Metadata\StructureMetadata;
22
use Sulu\Component\DocumentManager\DocumentManagerInterface;
23
use Sulu\Component\HttpCache\CacheLifetimeResolverInterface;
24
25
/**
26
 * Provides route-defaults for articles.
27
 */
28
class ArticleRouteDefaultProvider implements RouteDefaultsProviderInterface
29
{
30
    /**
31
     * @var DocumentManagerInterface
32
     */
33
    private $documentManager;
34
35
    /**
36
     * @var StructureMetadataFactoryInterface
37
     */
38
    private $structureMetadataFactory;
39
40
    /**
41
     * @var CacheLifetimeResolverInterface
42
     */
43
    private $cacheLifetimeResolver;
44
45
    /**
46
     * @var StructureManagerInterface
47
     */
48
    private $structureManager;
49
50
    /**
51
     * @param DocumentManagerInterface $documentManager
52
     * @param StructureMetadataFactoryInterface $structureMetadataFactory
53
     * @param CacheLifetimeResolverInterface $cacheLifetimeResolver
54
     * @param StructureManagerInterface $structureManager
55
     */
56 5
    public function __construct(
57
        DocumentManagerInterface $documentManager,
58
        StructureMetadataFactoryInterface $structureMetadataFactory,
59
        CacheLifetimeResolverInterface $cacheLifetimeResolver,
60
        StructureManagerInterface $structureManager
61
    ) {
62 5
        $this->documentManager = $documentManager;
63 5
        $this->structureMetadataFactory = $structureMetadataFactory;
64 5
        $this->cacheLifetimeResolver = $cacheLifetimeResolver;
65 5
        $this->structureManager = $structureManager;
66 5
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @param ArticleDocument $object
72
     */
73 2
    public function getByEntity($entityClass, $id, $locale, $object = null)
74
    {
75 2
        if (!$object) {
76 2
            $object = $this->documentManager->find($id, $locale);
77
        }
78
79 2
        $pageNumber = $object->getPageNumber();
80 2
        if ($object instanceof ArticlePageDocument) {
81
            // the article contains the seo/excerpt data and the controller handles the page-number automatically
82 1
            $object = $object->getParent();
83
        }
84
85 2
        $metadata = $this->structureMetadataFactory->getStructureMetadata('article', $object->getStructureType());
86
87
        // this parameter should not be used
88
        // but the sulu-collector for the profiler needs it to determine data from request
89 2
        $structure = $this->structureManager->wrapStructure('article', $metadata);
0 ignored issues
show
Bug introduced by
It seems like $metadata defined by $this->structureMetadata...ct->getStructureType()) on line 85 can be null; however, Sulu\Component\Content\C...erface::wrapStructure() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
90 2
        $structure->setDocument($object);
91
92
        return [
93 2
            'object' => $object,
94 2
            'view' => $metadata->view,
95 2
            'pageNumber' => $pageNumber,
96 2
            'structure' => $structure,
97 2
            '_cacheLifetime' => $this->getCacheLifetime($metadata),
98 2
            '_controller' => $metadata->controller,
99
        ];
100
    }
101
102
    /**
103
     * If article is not published the document will be of typ unknown-document.
104
     * Also check the workflow stage if it`s a ArticleDocument.
105
     *
106
     * {@inheritdoc}
107
     */
108 3
    public function isPublished($entityClass, $id, $locale)
109
    {
110 3
        $object = $this->documentManager->find($id, $locale);
111
112 3
        return $object instanceof ArticleInterface && WorkflowStage::PUBLISHED === $object->getWorkflowStage();
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function supports($entityClass)
119
    {
120
        return $entityClass === ArticleDocument::class || $entityClass === ArticlePageDocument::class;
121
    }
122
123
    /**
124
     * Get cache life time.
125
     *
126
     * @param StructureMetadata $metadata
127
     *
128
     * @return int|null
129
     */
130 2
    private function getCacheLifetime($metadata)
131
    {
132 2
        $cacheLifetime = $metadata->cacheLifetime;
133
134 2
        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...
135
            return null;
136
        }
137
138 2
        if (!is_array($cacheLifetime)
139 2
            || !isset($cacheLifetime['type'])
140 2
            || !isset($cacheLifetime['value'])
141 2
            || !$this->cacheLifetimeResolver->supports($cacheLifetime['type'], $cacheLifetime['value'])
142
        ) {
143
            throw new \InvalidArgumentException(
144
                sprintf('Invalid cachelifetime in article route default provider: %s', var_export($cacheLifetime, true))
145
            );
146
        }
147
148 2
        return $this->cacheLifetimeResolver->resolve($cacheLifetime['type'], $cacheLifetime['value']);
149
    }
150
}
151