Completed
Push — master ( e81671...a22352 )
by André
93:06 queued 73:42
created

AbstractURLHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 5
lcom 2
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
getOptionsResolver() 0 1 ?
A setOptions() 0 8 2
A setUrlStatus() 0 14 2
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\Bundle\EzPublishCoreBundle\URLChecker\Handler;
8
9
use DateTime;
10
use Exception;
11
use eZ\Bundle\EzPublishCoreBundle\URLChecker\URLHandlerInterface;
12
use eZ\Publish\API\Repository\URLService;
13
use eZ\Publish\API\Repository\Values\URL\URL;
14
use Psr\Log\LoggerAwareTrait;
15
use Psr\Log\NullLogger;
16
17
abstract class AbstractURLHandler implements URLHandlerInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /** @var \eZ\Publish\API\Repository\URLService */
22
    protected $urlService;
23
24
    /** @var array */
25
    protected $options;
26
27
    public function __construct(URLService $urlService)
28
    {
29
        $this->logger = new NullLogger();
30
        $this->urlService = $urlService;
31
        $this->options = $this->getOptionsResolver()->resolve();
32
    }
33
34
    /**
35
     * Returns options resolver.
36
     *
37
     * @return \Symfony\Component\OptionsResolver\OptionsResolver
38
     */
39
    abstract protected function getOptionsResolver();
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setOptions(array $options = null)
45
    {
46
        if ($options === null) {
47
            $options = [];
48
        }
49
50
        $this->options = $this->getOptionsResolver()->resolve($options);
51
    }
52
53
    /**
54
     * Sets URL status.
55
     *
56
     * @param \eZ\Publish\API\Repository\Values\URL\URL $url
57
     * @param bool $isValid
58
     */
59
    protected function setUrlStatus(URL $url, $isValid)
60
    {
61
        try {
62
            $updateStruct = $this->urlService->createUpdateStruct();
63
            $updateStruct->isValid = $isValid;
64
            $updateStruct->lastChecked = new DateTime();
65
66
            $this->urlService->updateUrl($url, $updateStruct);
67
68
            $this->logger->info(sprintf('URL id = %d (%s) was checked (valid = %s)', $url->id, $url->url, (int) $isValid));
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...
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...
69
        } catch (Exception $e) {
70
            $this->logger->error(sprintf('Cannot update URL id = %d status: %s', $url->id, $url->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...
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...
71
        }
72
    }
73
}
74