Completed
Push — master ( cfe8d7...fcc746 )
by André
19:36 queued 06:48
created

URLService::findUsages()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 5
nop 3
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Repository;
8
9
use DateTime;
10
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
11
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
12
use eZ\Publish\API\Repository\URLService as URLServiceInterface;
13
use eZ\Publish\API\Repository\Values\Content\Query;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion as ContentCriterion;
15
use eZ\Publish\API\Repository\Values\URL\SearchResult;
16
use eZ\Publish\API\Repository\Values\URL\URL;
17
use eZ\Publish\API\Repository\Values\URL\URLQuery;
18
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
19
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;
20
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
21
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
22
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
23
use eZ\Publish\SPI\Persistence\URL\Handler as URLHandler;
24
use eZ\Publish\SPI\Persistence\URL\URL as SPIUrl;
25
use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct as SPIUrlUpdateStruct;
26
27
class URLService implements URLServiceInterface
28
{
29
    /**
30
     * @var \eZ\Publish\Core\Repository\Repository
31
     */
32
    protected $repository;
33
34
    /**
35
     * @var \eZ\Publish\SPI\Persistence\URL\Handler
36
     */
37
    protected $urlHandler;
38
39
    /**
40
     * URLService constructor.
41
     *
42
     * @param \eZ\Publish\API\Repository\Repository $repository
43
     * @param \eZ\Publish\SPI\Persistence\URL\Handler $urlHandler
44
     */
45
    public function __construct(RepositoryInterface $repository, URLHandler $urlHandler)
46
    {
47
        $this->repository = $repository;
0 ignored issues
show
Documentation Bug introduced by
$repository is of type object<eZ\Publish\API\Repository\Repository>, but the property $repository was declared to be of type object<eZ\Publish\Core\Repository\Repository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
48
        $this->urlHandler = $urlHandler;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function findUrls(URLQuery $query)
55
    {
56
        if ($this->repository->hasAccess('url', 'view') !== true) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead. Check if user has access to a given module / function. Low level function, use canUser instead if you have objects to check against.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
57
            throw new UnauthorizedException('url', 'view');
58
        }
59
60 View Code Duplication
        if ($query->offset !== null && !is_numeric($query->offset)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            throw new InvalidArgumentValue('offset', $query->offset);
62
        }
63
64 View Code Duplication
        if ($query->limit !== null && !is_numeric($query->limit)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            throw new InvalidArgumentValue('limit', $query->limit);
66
        }
67
68
        $results = $this->urlHandler->find($query);
69
70
        $items = [];
71
        foreach ($results['items'] as $url) {
72
            $items[] = $this->buildDomainObject($url);
73
        }
74
75
        return new SearchResult([
76
            'totalCount' => $results['count'],
77
            'items' => $items,
78
        ]);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 View Code Duplication
    public function updateUrl(URL $url, URLUpdateStruct $struct)
85
    {
86
        if ($this->repository->hasAccess('url', 'update') !== true) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead. Check if user has access to a given module / function. Low level function, use canUser instead if you have objects to check against.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
87
            throw new UnauthorizedException('url', 'update');
88
        }
89
90
        if (!$this->isUnique($url->id, $struct->url)) {
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
            throw new InvalidArgumentException('struct', 'url already exists');
92
        }
93
94
        $updateStruct = $this->buildUpdateStruct($this->loadById($url->id), $struct);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
96
        $this->repository->beginTransaction();
97
        try {
98
            $this->urlHandler->updateUrl($url->id, $updateStruct);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
            $this->repository->commit();
100
        } catch (\Exception $e) {
101
            $this->repository->rollback();
102
            throw $e;
103
        }
104
105
        return $this->loadById($url->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 View Code Duplication
    public function loadById($id)
112
    {
113
        if ($this->repository->hasAccess('url', 'view') !== true) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead. Check if user has access to a given module / function. Low level function, use canUser instead if you have objects to check against.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
114
            throw new UnauthorizedException('url', 'view');
115
        }
116
117
        return $this->buildDomainObject(
118
            $this->urlHandler->loadById($id)
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 View Code Duplication
    public function loadByUrl($url)
126
    {
127
        if ($this->repository->hasAccess('url', 'view') !== true) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead. Check if user has access to a given module / function. Low level function, use canUser instead if you have objects to check against.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
128
            throw new UnauthorizedException('url', 'view');
129
        }
130
131
        return $this->buildDomainObject(
132
            $this->urlHandler->loadByUrl($url)
133
        );
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function createUpdateStruct()
140
    {
141
        return new URLUpdateStruct();
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function findUsages(URL $url, $offset = 0, $limit = -1)
148
    {
149
        $contentIds = $this->urlHandler->findUsages($url->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
150
        if (empty($contentIds)) {
151
            return new UsageSearchResult();
152
        }
153
154
        $query = new Query();
155
        $query->filter = new ContentCriterion\LogicalAnd([
156
            new ContentCriterion\ContentId($contentIds),
157
            new ContentCriterion\Visibility(ContentCriterion\Visibility::VISIBLE),
158
        ]);
159
160
        $query->offset = $offset;
161
        if ($limit > -1) {
162
            $query->limit = $limit;
163
        }
164
165
        $searchResults = $this->repository->getSearchService()->findContentInfo($query);
166
167
        $usageResults = new UsageSearchResult();
168
        $usageResults->totalCount = $searchResults->totalCount;
169
        foreach ($searchResults->searchHits as $hit) {
170
            $usageResults->items[] = $hit->valueObject;
171
        }
172
173
        return $usageResults;
174
    }
175
176
    /**
177
     * Builds domain object from ValueObject returned by Persistence API.
178
     *
179
     * @param \eZ\Publish\SPI\Persistence\URL\URL $data
180
     * @return \eZ\Publish\API\Repository\Values\URL\URL
181
     */
182
    protected function buildDomainObject(SPIUrl $data)
183
    {
184
        return new URL([
185
            'id' => $data->id,
186
            'url' => $data->url,
187
            'isValid' => $data->isValid,
188
            'lastChecked' => $this->createDateTime($data->lastChecked),
189
            'created' => $this->createDateTime($data->created),
190
            'modified' => $this->createDateTime($data->modified),
191
        ]);
192
    }
193
194
    /**
195
     * Builds SPI update structure used by Persistence API.
196
     *
197
     * @param \eZ\Publish\API\Repository\Values\URL\URL $url
198
     * @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $data
199
     * @return \eZ\Publish\SPI\Persistence\URL\URLUpdateStruct
200
     */
201
    protected function buildUpdateStruct(URL $url, URLUpdateStruct $data)
202
    {
203
        $updateStruct = new SPIUrlUpdateStruct();
204
205
        if ($data->url !== null && $url->url !== $data->url) {
0 ignored issues
show
Documentation introduced by
The property $url is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
206
            $updateStruct->url = $data->url;
207
            // Reset URL validity
208
            $updateStruct->lastChecked = 0;
209
            $updateStruct->isValid = true;
210
        } else {
211
            $updateStruct->url = $url->url;
0 ignored issues
show
Documentation introduced by
The property $url is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
212
213
            if ($data->lastChecked !== null) {
214
                $updateStruct->lastChecked = $data->lastChecked->getTimestamp();
215
            } elseif ($data->lastChecked !== null) {
216
                $updateStruct->lastChecked = $url->lastChecked->getTimestamp();
0 ignored issues
show
Documentation introduced by
The property $lastChecked is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
217
            } else {
218
                $updateStruct->lastChecked = 0;
219
            }
220
221
            if ($data->isValid !== null) {
222
                $updateStruct->isValid = $data->isValid;
223
            } else {
224
                $updateStruct->isValid = $url->isValid;
0 ignored issues
show
Documentation introduced by
The property $isValid is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
225
            }
226
        }
227
228
        return $updateStruct;
229
    }
230
231
    /**
232
     * Check if URL is unique.
233
     *
234
     * @param int $id
235
     * @param string $url
236
     * @return bool
237
     * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
238
     */
239
    protected function isUnique($id, $url)
240
    {
241
        try {
242
            return $this->loadByUrl($url)->id === $id;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
243
        } catch (NotFoundException $e) {
244
            return true;
245
        }
246
    }
247
248
    private function createDateTime($timestamp)
249
    {
250
        if ($timestamp > 0) {
251
            return new DateTime("@{$timestamp}");
252
        }
253
254
        return null;
255
    }
256
}
257