PageTrait::setSlug()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 16
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 9.9
c 1
b 0
f 0
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
9
trait PageTrait
10
{
11
    /**
12
     * @ORM\Column(type="string", length=150)
13
     */
14
    protected $slug;
15
16
    /**
17
     * @ORM\Column(type="string", length=255, nullable=true)
18
     */
19
    protected $h1;
20
21
    public function getH1($elseTitle = false): ?string
22
    {
23
        if ($elseTitle) {
24
            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

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