Test Failed
Pull Request — master (#249)
by Mr.
04:49
created

PostCard::initOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Widget;
6
7
use App\Blog\Entity\Post;
8
use Yiisoft\Html\Html;
9
use Yiisoft\Router\UrlGeneratorInterface;
10
use Yiisoft\Yii\Bootstrap5\Widget;
11
12
class PostCard extends Widget
13
{
14
    private ?Post $post = null;
15
16
    private array $options = [];
17
18
    private UrlGeneratorInterface $urlGenerator;
19
20
    public function __construct(UrlGeneratorInterface $urlGenerator)
21
    {
22
        $this->urlGenerator = $urlGenerator;
23
    }
24
25
    protected function run(): string
26
    {
27
        if (!isset($this->options['id'])) {
28
            $this->options['id'] = "{$this->getId()}-post-card";
29
        }
30
31
        $this->initOptions();
32
33
        return implode("\n", [
34
            Html::openTag('div', $this->options),
35
            Html::openTag('div', ['class' => 'card-body d-flex flex-column align-items-start']),
36
            $this->renderHead(),
37
            $this->renderBody(),
38
            $this->renderTags(),
39
            Html::closeTag('div'),
40
            Html::closeTag('div'),
41
        ]);
42
    }
43
44
    protected function renderHead(): string
45
    {
46
        return Html::a(
47
            Html::encode($this->post->getTitle()),
48
            $this->urlGenerator->generate('blog/post', ['slug' => $this->post->getSlug()]),
49
            ['class' => 'mb-0 h4 text-decoration-none'] // stretched-link
50
        )
51
        ->render();
52
    }
53
54
    protected function renderBody(): string
55
    {
56
        $return = Html::openTag('div', ['class' => card-text mb-auto']);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting ',' or ']' on line 56 at column 61
Loading history...
57
        $return .= $this->post->getPublishedAt()->format('M, d');
58
        $return .= ' by ';
59
        $return .= Html::a(
60
            $this->post->getUser()->getLogin(),
61
            $this->urlGenerator->generate('user/profile', ['login' => $this->post->getUser()->getLogin()])
62
        )->class('mb-1 text-muted');
63
        
64
        $return .= Html::p(
65
            mb_substr($this->post->getContent(), 0, 400)
66
            . (mb_strlen($this->post->getContent()) > 400 ? '…' : '')
67
        );
68
        return $return . Html::closeTag('div');
69
    }
70
71
    protected function renderTags(): string
72
    {
73
        $return = Html::openTag('div', ['class' => 'mt-3']);
74
        foreach ($this->post->getTags() as $tag) {
75
            $return .= Html::a(
76
                Html::encode($tag->getLabel()),
77
                $this->urlGenerator->generate('blog/tag', ['label' => $tag->getLabel()]),
78
                ['class' => 'btn btn-outline-secondary btn-sm me-2 mt-1']
79
            );
80
        }
81
        return $return . Html::closeTag('div');
82
    }
83
84
    protected function initOptions(): void
85
    {
86
        Html::addCssClass($this->options, ['widget' => 'card mb-4']);
87
    }
88
89
    public function post(?Post $post): self
90
    {
91
        $this->post = $post;
92
93
        if ($post !== null) {
94
            $this->options['data']['post-slug'] = $post->getSlug();
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * The HTML attributes for the widget container tag. The following special options are recognized.
102
     *
103
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
104
     */
105
    public function options(array $value): self
106
    {
107
        $this->options = $value;
108
109
        return $this;
110
    }
111
}
112