Completed
Push — master ( 36782f...ab5f43 )
by Sergey
06:23 queued 04:02
created

TryIt::uploadImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\Response;
6
use seregazhuk\PinterestBot\Helpers\Pagination;
7
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
8
9
trait TryIt
10
{
11
    use HandlesRequest, UploadsImages;
12
13
    /**
14
     * @return array
15
     */
16
    protected function requiresLoginForTryIt()
17
    {
18
        return [
19
            'tryIt',
20
            'editTryIt',
21
            'deleteTryIt',
22
        ];
23
    }
24
25
    /**
26
     * @param string $pinId
27
     * @param array $additionalData
28
     * @param int $limit
29
     * @return Pagination
30
     */
31
    abstract protected function getAggregatedActivity($pinId, $additionalData = [], $limit);
32
33
    /**
34
     * Makes a DidIt activity record.
35
     *
36
     * @param string $pinId
37
     * @param string $comment
38
     * @param null|string $pathToImage
39
     * @return array
40
     */
41
    public function tryIt($pinId, $comment, $pathToImage = null)
42
    {
43
        $data = $this->makeRequest($pinId, $comment, $pathToImage);
44
45
        $this->post(UrlBuilder::RESOURCE_TRY_PIN_CREATE, $data);
46
47
        return $this->getResponse()->getResponseData();
48
    }
49
50
    /**
51
     * @param string $pinId
52
     * @param string $tryItRecordId
53
     * @param string $comment
54
     * @param string|null $pathToImage
55
     * @return bool|Response
56
     */
57
    public function editTryIt($pinId, $tryItRecordId, $comment, $pathToImage = null)
58
    {
59
        $data = $this->makeRequest($pinId, $comment, $pathToImage);
60
        $data['user_did_it_data_id'] = $tryItRecordId;
61
62
        return $this->post(UrlBuilder::RESOURCE_TRY_PIN_EDIT, $data);
63
    }
64
65
    /**
66
     * Get the pinners who have tied this pin
67
     *
68
     * @param string $pinId
69
     * @param int $limit
70
     * @return Pagination
71
     */
72
    public function tried($pinId, $limit = Pagination::DEFAULT_LIMIT)
73
    {
74
        $data = [
75
            'field_set_key'    => 'did_it',
76
            'show_did_it_feed' => true,
77
        ];
78
79
        return $this->getAggregatedActivity($pinId, $data, $limit);
80
    }
81
82
    /**
83
     * @param string $tryItRecordId
84
     * @return bool|Response
85
     */
86
    public function deleteTryIt($tryItRecordId)
87
    {
88
        return $this->post(
89
            UrlBuilder::RESOURCE_TRY_PIN_DELETE,
90
            ['user_did_it_data_id' => $tryItRecordId]
91
        );
92
    }
93
94
    /**
95
     * @param string $pinId
96
     * @param string $comment
97
     * @param string|null $pathToImage
98
     * @return array
99
     */
100
    protected function makeRequest($pinId, $comment, $pathToImage = null)
101
    {
102
        $data = [
103
            'pin_id'  => $pinId,
104
            'details' => $comment,
105
        ];
106
107
        // If an image was specified try to upload it first to Pinterest simple upload to
108
        // receive and image url. Then we upload it to special DidIt resource to
109
        // get an image signature for the request.
110
        if (!empty($pathToImage)) {
111
            $data['image_signature'] = $this->uploadImage($pathToImage);
112
        }
113
114
        return $data;
115
    }
116
117
    /**
118
     * @param string $pathToImage
119
     * @return string
120
     */
121
    protected function uploadImage($pathToImage)
122
    {
123
        $request = ['image_url' => $this->upload($pathToImage)];
124
125
        $this->post(UrlBuilder::RESOURCE_TRY_PIN_IMAGE_UPLOAD, $request);
126
127
        return $this->getResponse()->getResponseData('image_signature');
128
    }
129
}
130