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

MetafieldTrait::newMetafield()   A

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\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
     * @return string
19
     */
20
    protected function _metafieldType (): string {
21
        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...
22
    }
23
24
    /**
25
     * @return Metafield[]
26
     */
27
    public function getMetafields () {
28
        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

28
        assert($this->/** @scrutinizer ignore-call */ hasId());
Loading history...
29
        // the endpoints are all over the place.
30
        // querying the main directory works for all entities.
31
        $remote = $this->api->get('metafields', [
32
            '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

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