Completed
Pull Request — master (#202)
by Sam
01:51
created

StitchDataSender::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use Sminnee\StitchData\StitchApi;
4
5
/**
6
 * Sends package data to StitchData
7
 */
8
class StitchDataSender
9
{
10
11
    private $accessToken = null;
12
    private $clientID = null;
13
    private $tableName = null;
14
15
    public function __construct()
16
    {
17
        // Use environment defines for client ID and access token by default
18
        if (defined('STITCHDATA_CLIENT_ID')) {
19
            $this->clientID = STITCHDATA_CLIENT_ID;
20
        }
21
        if (defined('STITCHDATA_ACCESS_TOKEN')) {
22
            $this->accessToken = STITCHDATA_ACCESS_TOKEN;
23
        }
24
    }
25
26
    /**
27
     * Set the client ID of your StitchData account.
28
     * @return $this, for use with fluent syntax
0 ignored issues
show
Documentation introduced by
The doc-type $this, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     */
30
    public function setClientID($clientID)
31
    {
32
        $this->clientID = $clientID;
33
        return $this;
34
    }
35
36
    /**
37
     * Set the access token for the StitchData Import API
38
     * @return $this, for use with fluent syntax
0 ignored issues
show
Documentation introduced by
The doc-type $this, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
39
     */
40
    public function setAccessToken($accessToken)
41
    {
42
        $this->accessToken = $accessToken;
43
        return $this;
44
    }
45
46
    /**
47
     * Set the table name to write packages to
48
     * @return $this, for use with fluent syntax
0 ignored issues
show
Documentation introduced by
The doc-type $this, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
49
     */
50
    public function setTableName($tableName)
51
    {
52
        $this->tableName = $tableName;
53
        return $this;
54
    }
55
56
    /**
57
     * Get the client ID of your StitchData account.
58
     * @return string
59
     */
60
    public function getClientID()
61
    {
62
        return $this->clientID;
63
    }
64
65
    /**
66
     * Get the access token for the StitchData Import API
67
     * @return string
68
     */
69
    public function getAccessToken()
70
    {
71
        return $this->accessToken;
72
    }
73
74
    /**
75
     * Get the table name to write packages to
76
     * @return string
77
     */
78
    public function getTableName()
79
    {
80
        return $this->tableName;
81
    }
82
83
    /**
84
     * Send the given package to the StitchData API
85
     */
86
    public function sendAddon(Addon $package)
87
    {
88
        // If unconfigured, silently no-op
89
        if (!$this->clientID || !$this->accessToken || !$this->tableName) {
90
            return;
91
        }
92
93
        $stitch = new StitchApi($this->clientID, $this->accessToken);
94
95
        $stitch->pushRecords(
96
            $this->tableName,
97
            [ 'Name' ],
98
            [
99
                $this->addonToJson($package)
100
            ]
101
        );
102
    }
103
104
    public function addonToJson(Addon $package)
105
    {
106
        $tz = new DateTimeZone('UTC');
107
108
        $data = $package->toMap();
109
        unset($data['LastEdited']);
110
        unset($data['Created']);
111
        unset($data['ClassName']);
112
        unset($data['RecordClassName']);
113
114
        foreach (['Released', 'LastUpdated', 'LastBuilt'] as $field) {
115
            if ($package->$field) {
116
                $datetime = is_numeric($package->$field) ? "@" . $package->$field : $package->$field;
117
                $data[$field] = new Datetime($datetime, $tz);
118
            }
119
        }
120
121
        if ($package->RatingDetails) {
0 ignored issues
show
Documentation introduced by
The property RatingDetails does not exist on object<Addon>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122
            $data['RatingDetails'] = [];
123
            foreach (json_decode($package->RatingDetails, true) as $k => $v) {
0 ignored issues
show
Documentation introduced by
The property RatingDetails does not exist on object<Addon>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
                $data['RatingDetails'][] = [
125
                    'Name' => $k,
126
                    'Value' => $v,
127
                ];
128
            }
129
        }
130
131
        $data['Versions'] = [];
132
        foreach ($package->Versions() as $version) {
0 ignored issues
show
Bug introduced by
The method Versions() does not exist on Addon. Did you maybe mean SortedVersions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
133
            $versionData = $version->toMap();
134
            unset($versionData['LastEdited']);
135
            unset($versionData['Created']);
136
            unset($versionData['ClassName']);
137
            unset($versionData['RecordClassName']);
138
            unset($versionData['AddonID']);
139
140
            if ($version->Released) {
141
                $versionData['Released'] = new Datetime($version->Released, $tz);
142
            }
143
144
            foreach ($version->CompatibleVersions() as $compatible) {
145
                $versionData['CompatibleVersions'][] = [
146
                    "Version" => $compatible->Name,
147
                    "Major" => $compatible->Major,
148
                    "Minor" => $compatible->Minor,
149
                ];
150
            }
151
152
            foreach ($version->Authors() as $author) {
153
                $authorData = $author->toMap();
154
                unset($authorData['ID']);
155
                unset($authorData['ClassName']);
156
                unset($authorData['RecordClassName']);
157
                unset($authorData['LastEdited']);
158
                unset($authorData['Created']);
159
                $versionData['Authors'][] = $authorData;
160
            }
161
162
            $data['Versions'][] = $versionData;
163
        }
164
165
        return $data;
166
    }
167
}
168