Article::getCommentsCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Helix\Shopify\Blog;
4
5
use Helix\Shopify\Base\AbstractEntity;
6
use Helix\Shopify\Base\AbstractEntity\CrudTrait;
7
use Helix\Shopify\Base\AbstractEntity\MetafieldTrait;
8
use Helix\Shopify\Base\Data;
9
use Helix\Shopify\Blog;
10
use Helix\Shopify\Blog\Article\Comment;
11
use Helix\Shopify\Blog\Article\Image;
12
13
/**
14
 * A blog article.
15
 *
16
 * @see https://shopify.dev/docs/admin-api/rest/reference/online-store/article
17
 *
18
 * @method string       getAuthor           ()
19
 * @method string       getBlogId           () injected
20
 * @method string       getBodyHtml         ()
21
 * @method string       getCreatedAt        ()
22
 * @method string       getHandle           ()
23
 * @method null|Image   getImage            ()
24
 * @method string       getPublished        ()
25
 * @method string       getPublishedAt      ()
26
 * @method string       getSummaryHtml      ()
27
 * @method string       getTags             ()
28
 * @method null|string  getTemplateSuffix   ()
29
 * @method string       getTitle            ()
30
 * @method string       getUpdatedAt        ()
31
 * @method string       getUserId           ()
32
 *
33
 * @method $this        setAuthor           (string $author)
34
 * @method $this        setBodyHtml         (string $html)
35
 * @method $this        setHandle           (string $handle)
36
 * @method $this        setImage            (?Image $image)
37
 * @method $this        setPublished        (string $published)
38
 * @method $this        setPublishedAt      (string $iso8601)
39
 * @method $this        setSummaryHtml      (string $html)
40
 * @method $this        setTags             (string $tags)
41
 * @method $this        setTemplateSuffix   (?string $suffix)
42
 * @method $this        setTitle            (string $title)
43
 *
44
 * @method Comment[]    selectComments      (callable $filter) `fn( Comment $comment ): bool`
45
 */
46
class Article extends AbstractEntity
47
{
48
49
    use CrudTrait;
50
    use MetafieldTrait;
51
52
    const TYPE = 'article';
53
    const DIR = 'articles';
54
55
    const MAP = [
56
        'image' => Data::class
57
    ];
58
59
    protected function _container()
60
    {
61
        return $this->getBlog();
62
    }
63
64
    /**
65
     * @return Blog
66
     */
67
    public function getBlog()
68
    {
69
        return Blog::load($this, $this->getBlogId());
70
    }
71
72
    /**
73
     * @return Comment[]
74
     */
75
    public function getComments()
76
    {
77
        assert($this->hasId());
78
        return Comment::loadAll($this, 'comments', [
79
            'blog_id' => $this->getBlogId(),
80
            'article_id' => $this->getId()
81
        ]);
82
    }
83
84
    /**
85
     * @return int
86
     */
87
    public function getCommentsCount(): int
88
    {
89
        assert($this->hasId());
90
        return $this->api->get("comments/count", [
91
            'blog_id' => $this->getBlogId(),
92
            'article_id' => $this->getId()
93
        ])['count'];
94
    }
95
96
    /**
97
     * Factory.
98
     *
99
     * @return Comment
100
     */
101
    public function newComment()
102
    {
103
        assert($this->hasId());
104
        return $this->api->factory($this, Comment::class, [
105
            'blog_id' => $this->getBlogId(),
106
            'article_id' => $this->getId()
107
        ]);
108
    }
109
}