TryIt   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 3
Metric Value
eloc 27
c 6
b 1
f 3
dl 0
loc 118
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteTryIt() 0 4 1
A editTryIt() 0 6 1
A requiresLoginForTryIt() 0 6 1
A makeRequest() 0 15 2
A uploadImage() 0 7 1
A tried() 0 8 1
A tryIt() 0 7 1
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, array $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();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getResponse()->getResponseData() also could return the type boolean which is incompatible with the documented return type array.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post(sereg...CE_TRY_PIN_EDIT, $data) also could return the type array which is incompatible with the documented return type boolean|seregazhuk\PinterestBot\Api\Response.
Loading history...
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(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->post(sereg...id' => $tryItRecordId)) also could return the type array which is incompatible with the documented return type boolean|seregazhuk\PinterestBot\Api\Response.
Loading history...
89
            UrlBuilder::RESOURCE_TRY_PIN_DELETE, ['user_did_it_data_id' => $tryItRecordId]
90
        );
91
    }
92
93
    /**
94
     * @param string $pinId
95
     * @param string $comment
96
     * @param string|null $pathToImage
97
     * @return array
98
     */
99
    protected function makeRequest($pinId, $comment, $pathToImage = null)
100
    {
101
        $data = [
102
            'pin_id'  => $pinId,
103
            'details' => $comment,
104
        ];
105
106
        // If an image was specified try to upload it first to Pinterest simple upload to
107
        // receive and image url. Then we upload it to special DidIt resource to
108
        // get an image signature for the request.
109
        if ($pathToImage !== null) {
110
            $data['image_signature'] = $this->uploadImage($pathToImage);
111
        }
112
113
        return $data;
114
    }
115
116
    /**
117
     * @param string $pathToImage
118
     * @return string
119
     */
120
    protected function uploadImage($pathToImage)
121
    {
122
        $request = ['image_url' => $this->upload($pathToImage)];
123
124
        $this->post(UrlBuilder::RESOURCE_TRY_PIN_IMAGE_UPLOAD, $request);
125
126
        return $this->getResponse()->getResponseData('image_signature');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRespons...Data('image_signature') returns the type array|boolean which is incompatible with the documented return type string.
Loading history...
127
    }
128
}
129