Passed
Push — master ( 542bba...4c863a )
by Dev
13:56
created

PageTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 20.93%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 123
ccs 9
cts 43
cp 0.2093
rs 10
c 1
b 0
f 0
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setH1() 0 5 1
A setUpdatedAt() 0 5 1
A setMainContent() 0 5 1
A setSlug() 0 16 4
A getCreatedAt() 0 3 1
A setCreatedAt() 0 5 1
A getMainContent() 0 3 1
A getRealSlug() 0 7 2
A getUpdatedAt() 0 3 1
A getH1() 0 7 2
A __toString() 0 3 1
A __constructPage() 0 5 3
A getSlug() 0 5 2
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Cocur\Slugify\Slugify;
6
use Doctrine\ORM\Mapping as ORM;
7
use PiedWeb\CMSBundle\Service\ShortCodeConverter;
8
use Symfony\Component\Validator\Constraints as Assert;
9
10
trait PageTrait
11
{
12
    /**
13
     * @ORM\Column(type="string", length=150)
14
     */
15
    protected $slug;
16
17
    /**
18
     * @ORM\Column(type="string", length=255, nullable=true)
19
     */
20
    protected $h1;
21
22
    public function getH1($elseTitle = false): ?string
23
    {
24
        if ($elseTitle) {
25
            return ShortCodeConverter::do($this->h1 ?? $this->title ?? '', $this->getLocale());
0 ignored issues
show
Bug introduced by
It seems like getLocale() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            return ShortCodeConverter::do($this->h1 ?? $this->title ?? '', $this->/** @scrutinizer ignore-call */ getLocale());
Loading history...
26
        }
27
28
        return $this->h1;
29
    }
30
31
    public function setH1(?string $h1): self
32
    {
33
        $this->h1 = $h1;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @ORM\Column(type="text", nullable=true)
40
     */
41
    protected $mainContent;
42
43
    /**
44
     * @ORM\Column(type="datetime")
45
     */
46
    protected $createdAt;
47
48 3
    /**
49
     * @ORM\Column(type="datetime")
50 3
     */
51 3
    protected $updatedAt;
52 3
53
    public function __toString()
54
    {
55
        return trim($this->host.'/'.$this->slug.' ');
56
    }
57
58
    public function __constructPage()
59
    {
60
        $this->updatedAt = null !== $this->updatedAt ? $this->updatedAt : new \DateTime();
61
        $this->createdAt = null !== $this->createdAt ? $this->createdAt : new \DateTime();
62
        $this->slug = '';
63
    }
64
65
    public function getSlug(): ?string
66
    {
67
        if (!$this->slug) return $this->id;
68
69
        return $this->slug;
70
    }
71
72
    public function getRealSlug(): ?string
73
    {
74
        if ('homepage' == $this->slug) {
75
            return '';
76
        }
77
78
        return $this->slug;
79
    }
80
81
    public function setSlug($slug, $set = false): self
82
    {
83
        if (true === $set) {
84
            $this->slug = $slug;
85
        } elseif (null === $slug) { // work around for disabled input in sonata admin
86 3
            if (null === $this->slug) {
87
                throw new \ErrorException('slug cant be null');
88 3
            }
89
        } else {
90
            $slg = new Slugify(['regexp' => '/[^A-Za-z0-9\/]+/']);
91 3
            $slug = $slg->slugify($slug);
92
            $slug = trim($slug, '/');
93 3
            $this->slug = $slug; //$this->setSlug(trim($slug, '/'), true);
94
        }
95 3
96
        return $this;
97
    }
98
99
    public function getMainContent(): ?string
100
    {
101
        return $this->mainContent;
102
    }
103
104
    public function setMainContent(?string $mainContent): self
105
    {
106
        $this->mainContent = $mainContent;
107
108
        return $this;
109
    }
110
111
    public function getCreatedAt(): ?\DateTimeInterface
112
    {
113
        return $this->createdAt;
114
    }
115
116
    public function setCreatedAt(\DateTimeInterface $createdAt): self
117
    {
118
        $this->createdAt = $createdAt;
119
120
        return $this;
121
    }
122
123
    public function getUpdatedAt(): ?\DateTimeInterface
124
    {
125
        return $this->updatedAt;
126
    }
127
128
    public function setUpdatedAt(\DateTimeInterface $updatedAt): self
129
    {
130
        $this->updatedAt = $updatedAt;
131
132
        return $this;
133
    }
134
}
135