Completed
Push — master ( e814e0...e87053 )
by Mārtiņš
01:53
created

Inktale   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 107
rs 10

6 Methods

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