BasicSeoGenerator::setKeywords()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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\TitleSeoInterface;
9
use Leogout\Bundle\SeoBundle\Seo\DescriptionSeoInterface;
10
use Leogout\Bundle\SeoBundle\Seo\KeywordsSeoInterface;
11
12
/**
13
 * Description of BasicSeoGenerator.
14
 *
15
 * @author: leogout
16
 */
17
class BasicSeoGenerator extends AbstractSeoGenerator
18
{
19
    /**
20
     * @param string $content
21
     *
22
     * @return $this
23
     */
24
    public function setTitle($content)
25
    {
26
        $this->tagBuilder->setTitle($content);
27
28
        return $this;
29
    }
30
31
    /**
32
     * @return TitleTag
33
     */
34
    public function getTitle()
35
    {
36
        return $this->tagBuilder->getTitle();
37
    }
38
39
    /**
40
     * @param string $content
41
     *
42
     * @return $this
43
     */
44
    public function setDescription($content)
45
    {
46
        $this->tagBuilder->addMeta('description')
47
            ->setType(MetaTag::NAME_TYPE)
48
            ->setValue('description')
49
            ->setContent((string) $content);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return MetaTag
56
     */
57
    public function getDescription()
58
    {
59
        return $this->tagBuilder->getMeta('description');
60
    }
61
62
    /**
63
     * @param string $keywords
64
     *
65
     * @return $this
66
     */
67
    public function setKeywords($keywords)
68
    {
69
        $this->tagBuilder->addMeta('keywords')
70
            ->setType(MetaTag::NAME_TYPE)
71
            ->setValue('keywords')
72
            ->setContent((string) $keywords);
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return MetaTag
79
     */
80
    public function getKeywords()
81
    {
82
        return $this->tagBuilder->getMeta('keywords');
83
    }
84
85
    /**
86
     * @param bool $shouldIndex
87
     * @param bool $shouldFollow
88
     *
89
     * @return $this
90
     */
91
    public function setRobots($shouldIndex, $shouldFollow)
92
    {
93
        $index = $shouldIndex ? 'index' : 'noindex';
94
        $follow = $shouldFollow ? 'follow' : 'nofollow';
95
96
        $this->tagBuilder->addMeta('robots')
97
            ->setType(MetaTag::NAME_TYPE)
98
            ->setValue('robots')
99
            ->setContent(sprintf('%s, %s', $index, $follow));
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param string $url
106
     *
107
     * @return $this
108
     */
109
    public function setCanonical($url)
110
    {
111
        $this->tagBuilder->addLink('canonical')
112
            ->setHref((string) $url)
113
            ->setRel('canonical');
114
115
        return $this;
116
    }
117
118
    /**
119
     * Generate seo tags from given resource.
120
     *
121
     * @param TitleSeoInterface|DescriptionSeoInterface|KeywordsSeoInterface $resource
122
     *
123
     * @return $this
124
     */
125 View Code Duplication
    public function fromResource($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
126
    {
127
        if ($resource instanceof TitleSeoInterface) {
128
            $this->setTitle($resource->getSeoTitle());
129
        }
130
        if ($resource instanceof DescriptionSeoInterface) {
131
            $this->setDescription($resource->getSeoDescription());
132
        }
133
        if ($resource instanceof KeywordsSeoInterface) {
134
            $this->setKeywords($resource->getSeoKeywords());
135
        }
136
137
        return $this;
138
    }
139
}
140