Completed
Pull Request — develop (#8)
by
unknown
07:18
created

BasicSeoGenerator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 7
dl 0
loc 185
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setTitle() 0 6 1
A getTitle() 0 4 1
A setDescription() 0 9 1
A getDescription() 0 4 1
A setKeywords() 0 9 1
A getKeywords() 0 4 1
A setRobots() 0 12 3
A setCanonical() 0 8 1
A setPreviousUrl() 0 8 1
A getPreviousUrl() 0 4 1
A setNextUrl() 0 8 1
A getNextUrl() 0 4 1
B fromResource() 0 32 8
1
<?php
2
3
namespace Leogout\Bundle\SeoBundle\Seo\Basic;
4
5
use Leogout\Bundle\SeoBundle\Model\MetaTag;
6
use Leogout\Bundle\SeoBundle\Model\TitleTag;
7
use Leogout\Bundle\SeoBundle\Seo\AbstractSeoGenerator;
8
use Leogout\Bundle\SeoBundle\Seo\Stdlib\PaginationAwareInterface;
9
use Leogout\Bundle\SeoBundle\Seo\Stdlib\ResourceInterface;
10
use Leogout\Bundle\SeoBundle\Seo\TitleSeoInterface;
11
use Leogout\Bundle\SeoBundle\Seo\DescriptionSeoInterface;
12
use Leogout\Bundle\SeoBundle\Seo\KeywordsSeoInterface;
13
14
/**
15
 * Description of BasicSeoGenerator.
16
 *
17
 * @author: leogout
18
 */
19
class BasicSeoGenerator extends AbstractSeoGenerator
20
{
21
    /**
22
     * @param string $content
23
     *
24
     * @return $this
25
     */
26
    public function setTitle($content)
27
    {
28
        $this->tagBuilder->setTitle($content);
29
30
        return $this;
31
    }
32
33
    /**
34
     * @return TitleTag
35
     */
36
    public function getTitle()
37
    {
38
        return $this->tagBuilder->getTitle();
39
    }
40
41
    /**
42
     * @param string $content
43
     *
44
     * @return $this
45
     */
46
    public function setDescription($content)
47
    {
48
        $this->tagBuilder->addMeta('description')
49
            ->setType(MetaTag::NAME_TYPE)
50
            ->setTagName('description')
51
            ->setContent((string) $content);
52
53
        return $this;
54
    }
55
56
    /**
57
     * @return MetaTag
58
     */
59
    public function getDescription()
60
    {
61
        return $this->tagBuilder->getMeta('description');
62
    }
63
64
    /**
65
     * @param string $keywords
66
     *
67
     * @return $this
68
     */
69
    public function setKeywords($keywords)
70
    {
71
        $this->tagBuilder->addMeta('keywords')
72
            ->setType(MetaTag::NAME_TYPE)
73
            ->setTagName('keywords')
74
            ->setContent((string) $keywords);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return MetaTag
81
     */
82
    public function getKeywords()
83
    {
84
        return $this->tagBuilder->getMeta('keywords');
85
    }
86
87
    /**
88
     * @param bool $shouldIndex
89
     * @param bool $shouldFollow
90
     *
91
     * @return $this
92
     */
93
    public function setRobots($shouldIndex, $shouldFollow)
94
    {
95
        $index = $shouldIndex ? 'index' : 'noindex';
96
        $follow = $shouldFollow ? 'follow' : 'nofollow';
97
98
        $this->tagBuilder->addMeta('robots')
99
            ->setType(MetaTag::NAME_TYPE)
100
            ->setTagName('robots')
101
            ->setContent(sprintf('%s, %s', $index, $follow));
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param string $url
108
     *
109
     * @return $this
110
     */
111
    public function setCanonical($url)
112
    {
113
        $this->tagBuilder->addLink('canonical')
114
            ->setHref((string) $url)
115
            ->setRel('canonical');
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $url
122
     *
123
     * @return $this
124
     */
125
    public function setPreviousUrl($url)
126
    {
127
        $this->tagBuilder->addLink('previousUrl')
128
            ->setHref((string) $url)
129
            ->setRel('prev');
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return \Leogout\Bundle\SeoBundle\Model\LinkTag|null
136
     */
137
    public function getPreviousUrl()
138
    {
139
        return $this->tagBuilder->getLink('previousUrl');
140
    }
141
142
    /**
143
     * @param string $url
144
     *
145
     * @return $this
146
     */
147
    public function setNextUrl($url)
148
    {
149
        $this->tagBuilder->addLink('nextUrl')
150
            ->setHref((string) $url)
151
            ->setRel('next');
152
153
        return $this;
154
    }
155
156
    /**
157
     * @return \Leogout\Bundle\SeoBundle\Model\LinkTag|null
158
     */
159
    public function getNextUrl()
160
    {
161
        return $this->tagBuilder->getLink('nextUrl');
162
    }
163
164
    /**
165
     * Generate seo tags from given resource.
166
     *
167
     * @param TitleSeoInterface|DescriptionSeoInterface|KeywordsSeoInterface $resource
168
     *
169
     * @return $this
170
     */
171
    public function fromResource($resource)
172
    {
173
        if ($resource instanceof TitleSeoInterface) {
174
            // backward compatibility
175
            $this->setTitle($resource->getSeoTitle());
176
        }
177
        if ($resource instanceof DescriptionSeoInterface) {
178
            // backward compatibility
179
            $this->setDescription($resource->getSeoDescription());
180
        }
181
        if ($resource instanceof KeywordsSeoInterface) {
182
            // backward compatibility
183
            $this->setKeywords($resource->getSeoKeywords());
184
        }
185
186
        // Pagination
187
        if ($resource instanceof PaginationAwareInterface) {
188
            $this->setPreviousUrl($resource->getPreviousUrl());
189
            $this->setNextUrl($resource->getPreviousUrl());
190
        }
191
192
        // Resource
193
        if ($resource instanceof ResourceInterface) {
194
            $this->setTitle($resource->getTitle());
195
            $this->setDescription($resource->getDescription());
196
            if ($keywords = $resource->getKeywords()) {
197
                $this->setKeywords((is_array($keywords)) ? $keywords : [$keywords]);
0 ignored issues
show
Documentation introduced by
is_array($keywords) ? $keywords : array($keywords) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
198
            }
199
        }
200
201
        return $this;
202
    }
203
}
204