Completed
Push — master ( 93b826...c2eec1 )
by Taosikai
11:32
created

AbstractManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the slince/shopify-api-php
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Slince\Shopify\Common\Manager;
13
14
use Slince\Shopify\Client;
15
use Slince\Shopify\Common\Model\ModelInterface;
16
17
abstract class AbstractManager implements ManagerInterface
18
{
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
29
    /**
30
     * Gets the model class.
31
     *
32
     * @return string
33
     */
34
    abstract public function getModelClass();
35
36
    /**
37
     * Gets the resource name.
38
     *
39
     * @return string
40
     */
41
    abstract public function getResourceName();
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getClient()
47
    {
48
        return $this->client;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function fromArray(array $data, $modelClass = null)
55
    {
56
        if (null === $modelClass) {
57
            $modelClass = $this->getModelClass();
58
        }
59
60
        return $this->client->getHydrator()->hydrate($modelClass, $data);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function createMany(array $data, $modelClass = null)
67
    {
68
        $models = [];
69
        foreach ($data as $item) {
70
            $models[] = $this->fromArray($item, $modelClass);
71
        }
72
73
        return $models;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function toArray($model)
80
    {
81
        return $this->client->getHydrator()->extract($model);
82
    }
83
}