Completed
Push — master ( 7a819f...689625 )
by Mārtiņš
01:54
created

Inktale::createArtwork()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 15
nc 4
nop 1
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\ProductItem;
10
use Inktale\Api\Structures\StoreItem;
11
12
class Inktale
13
{
14
    /** @var InktaleApiClient */
15
    private $api;
16
17
    /**
18
     * @param InktaleApiClient|null $apiClient
19
     */
20
    public function __construct(InktaleApiClient $apiClient = null)
21
    {
22
        $this->api = $apiClient ?: new InktaleApiClient;
23
    }
24
25
    /**
26
     * Set api key to be used for next requests
27
     *
28
     * @param string|null $apiKey
29
     */
30
    public function setApiKey($apiKey)
31
    {
32
        $this->api->setApiKey($apiKey);
33
    }
34
35
    /**
36
     * Create a new store and retrieve the api key. This request does not require an api key to be set
37
     *
38
     * @param string $email
39
     * @param string $name
40
     *
41
     * @return StoreItem
42
     * @throws InktaleApiException
43
     */
44
    public function createStore($email, $name)
45
    {
46
        $response = $this->api->post('store', [
47
            'email' => $email,
48
            'name' => $name,
49
        ]);
50
51
        return StoreItem::fromArray($response['store']);
52
    }
53
54
    /**
55
     * Fetch list of available products that artwork can be created on
56
     *
57
     * @return ProductItem[]
58
     * @throws InktaleApiException
59
     */
60
    public function getProducts()
61
    {
62
        $response = $this->api->get('products');
63
64
        $re = [];
65
        foreach ($response['products'] as $v) {
66
            $re[] = ProductItem::fromArray($v);
67
        }
68
69
        return $re;
70
    }
71
72
    /**
73
     * Create a new artwork on given products.
74
     * Response is an artwork id.
75
     * WARNING! Artwork creation is asynchronous!
76
     *
77
     * @param ArtworkCreationParams $params
78
     * @return string
79
     */
80
    public function createArtwork(ArtworkCreationParams $params)
81
    {
82
        $productTypes = [];
83
84
        foreach ($params->getProducts() as $k => $v) {
85
            $productTypes[] = [
86
                'productType' => $k,
87
                'profit' => $v,
88
            ];
89
        };
90
91
        $data = [
92
            'url' => $params->imageUrl,
93
            'name' => $params->name,
94
            'publish' => $params->publish ? 1 : 0,
95
            'description' => $params->description,
96
            'productTypes' => $productTypes,
97
            'keywords' => $params->keywords,
98
        ];
99
100
        $response = $this->api->post('artwork', $data);
101
102
        return $response['artworkId'];
103
    }
104
}