Completed
Push — master ( e87053...ec70af )
by Mārtiņš
02:10
created

Inktale::getArtworks()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 1
nop 3
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
81
     */
82
    public function createArtwork(ArtworkCreationParams $params)
83
    {
84
        $productTypes = [];
85
86
        foreach ($params->getProducts() as $k => $v) {
87
            $productTypes[] = [
88
                'productType' => $k,
89
                'profit' => $v,
90
            ];
91
        };
92
93
        $data = [
94
            'url' => $params->imageUrl,
95
            'name' => $params->name,
96
            'publish' => $params->publish ? 1 : 0,
97
            'description' => $params->description,
98
            'productTypes' => $productTypes,
99
            'keywords' => $params->keywords,
100
        ];
101
102
        $response = $this->api->post('artwork', $data);
103
104
        return $response['artworkId'];
105
    }
106
107
    /**
108
     * Get an artwork with files and products
109
     *
110
     * @param string $artworkId
111
     * @return ArtworkItem
112
     * @throws InktaleApiException
113
     */
114
    public function getArtwork($artworkId)
115
    {
116
        $response = $this->api->get('artwork/' . $artworkId);
117
118
        return ArtworkItem::fromArray($response['artwork']);
119
    }
120
121
    /**
122
     * Get a list of artworks
123
     *
124
     * @param int $page Page number (starting from 1)
125
     * @param int $perPage Items per page, up to 100
126
     * @param bool $publishedOnly
127
     * @return ArtworkListItem
128
     */
129
    public function getArtworks($page, $perPage, $publishedOnly = false)
130
    {
131
        $response = $this->api->get('artworks', [
132
            'page' => $page,
133
            'perPage' => $perPage,
134
            'publishedOnly' => $publishedOnly ? 1 : 0,
135
        ]);
136
137
        return ArtworkListItem::fromArray($response);
138
    }
139
}