MetafieldTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetafields() 0 12 1
A _metafieldType() 0 3 1
A newMetafield() 0 8 1
1
<?php
2
3
namespace Helix\Shopify\Base\AbstractEntity;
4
5
use Helix\Shopify\Base\AbstractEntity;
6
use Helix\Shopify\Metafield;
7
8
/**
9
 * Adds metafields to an entity.
10
 *
11
 * @see https://help.shopify.com/en/api/reference/metafield
12
 *
13
 * @mixin AbstractEntity
14
 */
15
trait MetafieldTrait
16
{
17
18
    /**
19
     * @return string
20
     */
21
    protected function _metafieldType(): string
22
    {
23
        return static::TYPE;
0 ignored issues
show
Bug introduced by
The constant Helix\Shopify\Base\Abstr...ty\MetafieldTrait::TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
    }
25
26
    /**
27
     * @return Metafield[]
28
     */
29
    public function getMetafields()
30
    {
31
        assert($this->hasId());
0 ignored issues
show
Bug introduced by
It seems like hasId() 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

31
        assert($this->/** @scrutinizer ignore-call */ hasId());
Loading history...
32
        // the endpoints are all over the place.
33
        // querying the main directory works for all entities.
34
        $remote = $this->api->get('metafields', [
35
            'metafield[owner_id]' => $this->getId(),
0 ignored issues
show
Bug introduced by
It seems like getId() 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

35
            'metafield[owner_id]' => $this->/** @scrutinizer ignore-call */ getId(),
Loading history...
36
            'metafield[owner_resource]' => $this->_metafieldType()
37
        ]);
38
        /** @var AbstractEntity $that */
39
        $that = $this;
40
        return $this->api->factoryAll($that, Metafield::class, $remote['metafields']);
41
    }
42
43
    /**
44
     * Factory.
45
     *
46
     * @return Metafield
47
     */
48
    public function newMetafield()
49
    {
50
        assert($this->hasId());
51
        /** @var AbstractEntity $that */
52
        $that = $this;
53
        return $this->api->factory($that, Metafield::class, [
54
            'owner_resource' => $this->_metafieldType(),
55
            'owner_id' => $this->getId()
56
        ]);
57
    }
58
}