Completed
Push — master ( 118874...7ee575 )
by Sergey
07:34 queued 05:25
created

TryIt::tried()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 9.6666
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
     * Makes a DidIt activity record.
15
     *
16
     * @param string $pinId
17
     * @param string $comment
18
     * @param null|string $pathToImage
19
     * @return array
20
     */
21
    public function tryIt($pinId, $comment, $pathToImage = null)
22
    {
23
        $data = $this->makeRequest($pinId, $comment, $pathToImage);
24
25
        $this->post($data, UrlBuilder::RESOURCE_TRY_PIN_CREATE);
26
27
        return $this->getResponse()->getResponseData();
28
    }
29
30
    /**
31
     * @param string $pinId
32
     * @param string $tryItRecordId
33
     * @param string $comment
34
     * @param string|null $pathToImage
35
     * @return bool|Response
36
     */
37
    public function editTryIt($pinId, $tryItRecordId, $comment, $pathToImage = null)
38
    {
39
        $data = $this->makeRequest($pinId, $comment, $pathToImage);
40
        $data['user_did_it_data_id'] = $tryItRecordId;
41
42
        return $this->post($data, UrlBuilder::RESOURCE_TRY_PIN_EDIT);
43
    }
44
45
    /**
46
     * Get the pinners who have tied this pin
47
     *
48
     * @param string $pinId
49
     * @param int $limit
50
     * @return Pagination
51
     */
52
    public function tried($pinId, $limit = Pagination::DEFAULT_LIMIT)
53
    {
54
        $data = [
55
            'field_set_key'    => 'did_it',
56
            'show_did_it_feed' => true,
57
        ];
58
59
        return $this->getAggregatedActivity($pinId, $data, $limit);
0 ignored issues
show
Bug introduced by
It seems like getAggregatedActivity() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
    }
61
62
    /**
63
     * @param string $tryItRecordId
64
     * @return bool|Response
65
     */
66
    public function deleteTryIt($tryItRecordId)
67
    {
68
        return $this->post(
69
            ['user_did_it_data_id' => $tryItRecordId],
70
            UrlBuilder::RESOURCE_TRY_PIN_DELETE
71
        );
72
    }
73
74
    /**
75
     * @param string $pinId
76
     * @param string $comment
77
     * @param string|null $pathToImage
78
     * @return array
79
     */
80
    protected function makeRequest($pinId, $comment, $pathToImage = null)
81
    {
82
        $data = [
83
            'pin_id'  => $pinId,
84
            'details' => $comment,
85
        ];
86
87
        // If an image was specified try to upload it first to Pinterest simple upload to
88
        // receive and image url. Then we upload it to special DidIt resource to
89
        // get an image signature for the request.
90
        if ($pathToImage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pathToImage of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
91
            $request = ['image_url' => $this->upload($pathToImage)];
92
93
            $this->post($request, UrlBuilder::RESOURCE_TRY_PIN_IMAGE_UPLOAD);
94
95
            $data['image_signatures'] = $this->getResponse()->getResponseData('image_signature');
96
        }
97
98
        return $data;
99
    }
100
}