Completed
Push — master ( 55ae25...96ccfa )
by Mārtiņš
01:54
created

Inktale::getApiClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace Inktale\Api;
5
6
7
use Inktale\Api\Exceptions\InktaleApiException;
8
use Inktale\Api\Params\ArtworkCreationParams;
9
use Inktale\Api\Structures\ArtworkItem;
10
use Inktale\Api\Structures\ArtworkListItem;
11
use Inktale\Api\Structures\ProductItem;
12
use Inktale\Api\Structures\StoreItem;
13
14
class Inktale
15
{
16
    /** @var InktaleApiClient */
17
    private $api;
18
19
    /**
20
     * @param InktaleApiClient|null $apiClient
21
     */
22
    public function __construct(InktaleApiClient $apiClient = null)
23
    {
24
        $this->api = $apiClient ?: new InktaleApiClient;
25
    }
26
27
    /**
28
     * Set api key to be used for next requests
29
     *
30
     * @param string|null $apiKey
31
     */
32
    public function setApiKey($apiKey)
33
    {
34
        $this->api->setApiKey($apiKey);
35
    }
36
37
    /**
38
     * Create a new store and retrieve the api key. This request does not require an api key to be set
39
     *
40
     * @param string $email
41
     * @param string $name
42
     *
43
     * @return StoreItem
44
     * @throws InktaleApiException
45
     */
46
    public function createStore($email, $name)
47
    {
48
        $response = $this->api->post('store', [
49
            'email' => $email,
50
            'name' => $name,
51
        ]);
52
53
        return StoreItem::fromArray($response['store']);
54
    }
55
56
    /**
57
     * Fetch list of available products that artwork can be created on
58
     *
59
     * @return ProductItem[]
60
     * @throws InktaleApiException
61
     */
62
    public function getProducts()
63
    {
64
        $response = $this->api->get('products');
65
66
        $re = [];
67
        foreach ($response['products'] as $v) {
68
            $re[] = ProductItem::fromArray($v);
69
        }
70
71
        return $re;
72
    }
73
74
    /**
75
     * Create a new artwork on given products.
76
     * Response is an artwork id.
77
     * WARNING! Artwork creation is asynchronous!
78
     *
79
     * @param ArtworkCreationParams $params
80
     * @return string Newly created artwork id
81
     * @throws InktaleApiException
82
     */
83
    public function createArtwork(ArtworkCreationParams $params)
84
    {
85
        $productTypes = [];
86
87
        foreach ($params->getProducts() as $k => $v) {
88
            $productTypes[] = [
89
                'productType' => $k,
90
                'profit' => $v,
91
            ];
92
        };
93
94
        $data = [
95
            'url' => $params->imageUrl,
96
            'name' => $params->name,
97
            'publish' => $params->publish ? 1 : 0,
98
            'description' => $params->description,
99
            'productTypes' => $productTypes,
100
            'keywords' => $params->keywords,
101
        ];
102
103
        $response = $this->api->post('artwork', $data);
104
105
        return $response['artworkId'];
106
    }
107
108
    /**
109
     * Get an artwork with files and products
110
     *
111
     * @param string $artworkId
112
     * @return ArtworkItem
113
     * @throws InktaleApiException
114
     */
115
    public function getArtwork($artworkId)
116
    {
117
        $response = $this->api->get('artwork/' . $artworkId);
118
119
        return ArtworkItem::fromArray($response['artwork']);
120
    }
121
122
    /**
123
     * Get a list of artworks
124
     *
125
     * @param int $page Page number (starting from 1)
126
     * @param int $perPage Items per page, up to 100
127
     * @param bool $publishedOnly
128
     * @return ArtworkListItem
129
     */
130
    public function getArtworks($page, $perPage, $publishedOnly = false)
131
    {
132
        $response = $this->api->get('artworks', [
133
            'page' => $page,
134
            'perPage' => $perPage,
135
            'publishedOnly' => $publishedOnly ? 1 : 0,
136
        ]);
137
138
        return ArtworkListItem::fromArray($response);
139
    }
140
141
    /**
142
     * Current API client instance
143
     *
144
     * @return InktaleApiClient
145
     */
146
    public function getApiClient()
147
    {
148
        return $this->api;
149
    }
150
}