Passed
Push — master ( 199ece...6bc7da )
by y
02:13
created

Article::getCommentsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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
    use CrudTrait;
49
    use MetafieldTrait;
50
51
    const TYPE = 'article';
52
    const DIR = 'articles';
53
54
    const MAP = [
55
        'image' => Data::class
56
    ];
57
58
    protected function _container () {
59
        return $this->getBlog();
60
    }
61
62
    /**
63
     * @return Blog
64
     */
65
    public function getBlog () {
66
        return Blog::load($this, $this->getBlogId());
67
    }
68
69
    /**
70
     * @return Comment[]
71
     */
72
    public function getComments () {
73
        assert($this->hasId());
74
        return Comment::loadAll($this, 'comments', [
75
            'blog_id' => $this->getBlogId(),
76
            'article_id' => $this->getId()
77
        ]);
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    public function getCommentsCount (): int {
84
        assert($this->hasId());
85
        return $this->api->get("comments/count", [
86
            'blog_id' => $this->getBlogId(),
87
            'article_id' => $this->getId()
88
        ])['count'];
89
    }
90
91
    /**
92
     * Factory.
93
     *
94
     * @return Comment
95
     */
96
    public function newComment () {
97
        assert($this->hasId());
98
        return $this->api->factory($this, Comment::class, [
99
            'blog_id' => $this->getBlogId(),
100
            'article_id' => $this->getId()
101
        ]);
102
    }
103
}