Completed
Push — master ( 6e4f15...3feaf0 )
by Alex
20s queued 11s
created

Media   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 119
ccs 45
cts 48
cp 0.9375
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUrlAttributeName() 0 4 1
A setUrlAttributeName() 0 4 1
A setProperty() 0 25 4
A setUrl() 0 4 1
A createMediaElement() 0 9 1
A initMedia() 0 6 1
A hasValue() 0 4 2
A addElement() 0 6 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Rule;
12
13
use FeedIo\Feed\Item;
14
use FeedIo\Feed\Item\MediaInterface;
15
use FeedIo\Feed\ItemInterface;
16
use FeedIo\Feed\NodeInterface;
17
use FeedIo\Parser\UrlGenerator;
18
use FeedIo\RuleAbstract;
19
20
class Media extends RuleAbstract
21
{
22
    const NODE_NAME = 'enclosure';
23
24
    const MRSS_NAMESPACE = "http://search.yahoo.com/mrss/";
25
26
    protected $urlAttributeName = 'url';
27
28
    /**
29
     * @var UrlGenerator
30
     */
31
    protected $urlGenerator;
32
33 24
    public function __construct(string $nodeName = null)
34
    {
35 24
        $this->urlGenerator = new UrlGenerator();
36 24
        parent::__construct($nodeName);
37 24
    }
38
39
    /**
40
     * @return string
41
     */
42 5
    public function getUrlAttributeName() : string
43
    {
44 5
        return $this->urlAttributeName;
45
    }
46
47
    /**
48
     * @param  string $name
49
     */
50 12
    public function setUrlAttributeName(string $name) : void
51
    {
52 12
        $this->urlAttributeName = $name;
53 12
    }
54
55
    /**
56
     * @param  NodeInterface $node
57
     * @param  \DOMElement   $element
58
     */
59 5
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
60
    {
61 5
        if ($node instanceof ItemInterface) {
62 5
            $media = $node->newMedia();
63 5
            $media->setNodeName($element->nodeName);
64
65 5
            switch ($element->nodeName) {
66 5
                case 'media:group':
67 1
                    $this->initMedia($media, $element);
68 1
                    $this->setUrl($media, $this->getChildAttributeValue($element, 'content', 'url', static::MRSS_NAMESPACE), $node);
69 1
                    break;
70 4
                case 'media:content':
71
                    $this->initMedia($media, $element);
72
                    $this->setUrl($media, $this->getAttributeValue($element, "url"), $node);
73
                    break;
74
                default:
75
                    $media
76 4
                        ->setType($this->getAttributeValue($element, 'type'))
77 4
                        ->setLength($this->getAttributeValue($element, 'length'));
78 4
                    $this->setUrl($media, $this->getAttributeValue($element, $this->getUrlAttributeName()), $node);
79 4
                    break;
80
            }
81 5
            $node->addMedia($media);
82
        }
83 5
    }
84
85
    /**
86
     * @param MediaInterface $media
87
     * @param string $url
88
     * @param NodeInterface $node
89
     */
90 5
    protected function setUrl(MediaInterface $media, string $url, NodeInterface $node): void
91
    {
92 5
        $media->setUrl($this->urlGenerator->getAbsolutePath($url, $node->getHost()));
93 5
    }
94
95
    /**
96
     * @param  \DomDocument   $document
97
     * @param  MediaInterface $media
98
     * @return \DomElement
99
     */
100 1
    public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement
101
    {
102 1
        $element = $document->createElement($this->getNodeName());
103 1
        $element->setAttribute($this->getUrlAttributeName(), $media->getUrl());
104 1
        $element->setAttribute('type', $media->getType() ?? '');
105 1
        $element->setAttribute('length', $media->getLength() ?? '');
106
107 1
        return $element;
108
    }
109
110
    /**
111
     * @param \MediaInterface $media
112
     * @param \DomElement $element
113
     */
114 1
    protected function initMedia(MediaInterface $media, \DOMElement $element): void
115
    {
116 1
        $media->setTitle($this->getChildValue($element, 'title', static::MRSS_NAMESPACE));
117 1
        $media->setDescription($this->getChildValue($element, 'description', static::MRSS_NAMESPACE));
118 1
        $media->setThumbnail($this->getChildAttributeValue($element, 'thumbnail', 'url', static::MRSS_NAMESPACE));
119 1
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 5
    protected function hasValue(NodeInterface $node) : bool
125
    {
126 5
        return $node instanceof ItemInterface && !! $node->getMedias();
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132 5
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
133
    {
134 5
        foreach ($node->getMedias() as $media) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FeedIo\Feed\NodeInterface as the method getMedias() does only exist in the following implementations of said interface: FeedIo\Feed\Item.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
135 1
            $rootElement->appendChild($this->createMediaElement($document, $media));
136
        }
137 5
    }
138
}
139