Inktale::createArtwork()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
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\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 Inktale profile name part: inktale.com/<name> Maybe be alpha-num
42
     * @param string $ipAddress Optional Used for timezone detection when subscribing email address to mailing lists
43
     *
44
     * @return StoreItem
45
     * @throws InktaleApiException
46
     */
47
    public function createStore($email, $name, $ipAddress = null)
48
    {
49
        $response = $this->api->post('store', [
50
            'email' => $email,
51
            'name' => $name,
52
            'ipAddress' => $ipAddress,
53
        ]);
54
55
        return StoreItem::fromArray($response['store']);
56
    }
57
58
    /**
59
     * Fetch list of available products that artwork can be created on
60
     *
61
     * @return ProductItem[]
62
     * @throws InktaleApiException
63
     */
64
    public function getProducts()
65
    {
66
        $response = $this->api->get('products');
67
68
        $re = [];
69
        foreach ($response['products'] as $v) {
70
            $re[] = ProductItem::fromArray($v);
71
        }
72
73
        return $re;
74
    }
75
76
    /**
77
     * Create a new artwork on given products.
78
     * Response is an artwork id.
79
     * WARNING! Artwork creation is asynchronous!
80
     *
81
     * @param ArtworkCreationParams $params
82
     * @return string Newly created artwork id
83
     * @throws InktaleApiException
84
     */
85
    public function createArtwork(ArtworkCreationParams $params)
86
    {
87
        $productTypes = [];
88
89
        foreach ($params->getProducts() as $k => $v) {
90
            $productTypes[] = [
91
                'productType' => $k,
92
                'profit' => $v,
93
            ];
94
        };
95
96
        $data = [
97
            'url' => $params->imageUrl,
98
            'name' => $params->name,
99
            'publish' => $params->publish ? 1 : 0,
100
            'description' => $params->description,
101
            'productTypes' => $productTypes,
102
            'keywords' => $params->keywords,
103
        ];
104
105
        $response = $this->api->post('artwork', $data);
106
107
        return $response['artworkId'];
108
    }
109
110
    /**
111
     * Get an artwork with files and products
112
     *
113
     * @param string $artworkId
114
     * @return ArtworkItem
115
     * @throws InktaleApiException
116
     */
117
    public function getArtwork($artworkId)
118
    {
119
        $response = $this->api->get('artwork/' . $artworkId);
120
121
        return ArtworkItem::fromArray($response['artwork']);
122
    }
123
124
    /**
125
     * Get a list of artworks
126
     *
127
     * @param int $page Page number (starting from 1)
128
     * @param int $perPage Items per page, up to 100
129
     * @param bool $publishedOnly
130
     * @return ArtworkListItem
131
     */
132
    public function getArtworks($page, $perPage, $publishedOnly = false)
133
    {
134
        $response = $this->api->get('artworks', [
135
            'page' => $page,
136
            'perPage' => $perPage,
137
            'publishedOnly' => $publishedOnly ? 1 : 0,
138
        ]);
139
140
        return ArtworkListItem::fromArray($response);
141
    }
142
143
    /**
144
     * Current API client instance
145
     *
146
     * @return InktaleApiClient
147
     */
148
    public function getApiClient()
149
    {
150
        return $this->api;
151
    }
152
}