Completed
Pull Request — master (#17)
by
unknown
08:03
created

Entity   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 91
ccs 20
cts 24
cp 0.8333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 14 6
A setLanguages() 0 4 1
A validateSettings() 0 4 1
A content() 0 7 2
A location() 0 7 2
1
<?php
2
3
namespace Kaliop\eZObjectWrapperBundle\Core;
4
5
use eZ\Publish\API\Repository\Values\Content\Location;
6
use eZ\Publish\API\Repository\Values\Content\Content;
7
use eZ\Publish\API\Repository\Repository as eZRepository;
8
use Kaliop\eZObjectWrapperBundle\Core\Traits\LoggingEntity;
9
use Kaliop\eZObjectWrapperBundle\Core\Traits\EntityManagerAwareEntity;
10
11
class Entity implements EntityInterface
12
{
13
    use LoggingEntity;
14
    use EntityManagerAwareEntity;
15
16
    /**
17
     * @var \eZ\Publish\API\Repository\Repository $repository
18
     */
19
    protected $repository;
20
    /**
21
     * @var \eZ\Publish\API\Repository\Values\Content\Location $location
22
     */
23
    protected $location;
24
    /**
25
     * @var \eZ\Publish\API\Repository\Values\Content\Content $content
26
     */
27
    protected $content;
28
29
    protected $settings;
30
31
    /**
32
     * Prioritized languages
33
     *
34
     * @var array
35
     */
36
    protected $languages;
37
38 10
    /**
39
     * @param eZRepository $repository
40 10
     * @param Content $content
41
     * @param Location $location
42
     * @param array $settings used to pass to the Entity instance anything that would be done via DIC if it was a service
43 10
     * @throws \InvalidArgumentException
44
     */
45
    public function __construct(eZRepository $repository, Content $content=null, Location $location=null, array $settings = array())
46
    {
47 10
        if ($content == null && $location == null) {
48 10
            throw new \InvalidArgumentException('Trying to create Entity with no content or location');
49 10
        }
50 10
        if ($content != null && $location != null) {
51 10
            if ($location->contentId != $content->id)
52
                throw new \InvalidArgumentException('Trying to create Entity with mismatching content and location');
53
        }
54
        $this->content = $content;
55
        $this->location = $location;
56
        $this->repository = $repository;
57
        $this->settings = $this->validateSettings($settings);
58
    }
59
60
    public function setLanguages(array $languages = null)
61 10
    {
62
        $this->languages = $languages;
0 ignored issues
show
Documentation Bug introduced by
It seems like $languages can be null. However, the property $languages is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
63 10
    }
64
65
    /**
66
     * Called from the constructor, with the settings received from the caller.
67
     * Subclasses can implement checking here, or merge the received settings with other data, using f.e. the Symfony
68
     * OptionsResolver component (see http://symfony.com/doc/current/components/options_resolver.html).
69
     *
70 9
     * @param array $settings
71
     * @return array
72 9
     */
73 3
    protected function validateSettings(array $settings)
74 3
    {
75 9
        return $settings;
76
    }
77
78
    /**
79
     * Return the content of the current location
80
     * @return \eZ\Publish\API\Repository\Values\Content\Content
81
     */
82 5
    public function content()
83
    {
84 5
        if($this->content == null){
85 5
            $this->content = $this->repository->getContentService()->loadContent($this->location->contentId, $this->languages);
86 5
        }
87 5
        return $this->content;
88
    }
89
90
    /**
91
     * NB: if entity has been created from a Content, returns its MAIN location. Otherwise, the Location set at creation
92
     * @return \eZ\Publish\API\Repository\Values\Content\Location
93
     */
94
    public function location()
95
    {
96
        if($this->location == null){
97
            $this->location = $this->repository->getLocationService()->loadLocation($this->content->contentInfo->mainLocationId);
98
        }
99
        return $this->location;
100
    }
101
}
102