StitchDataSender::addonToJson()   C
last analyzed

Complexity

Conditions 10
Paths 72

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 6.9406
c 0
b 0
f 0
cc 10
nc 72
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Sminnee\StitchData\StitchApi;
4
5
/**
6
 * Sends package data to StitchData
7
 */
8
class StitchDataSender
9
{
10
    /**
11
     * @var string
12
     */
13
    private $accessToken = null;
14
15
    /**
16
     * @var string
17
     */
18
    private $clientID = null;
19
20
    /**
21
     * @var string
22
     */
23
    private $tableName = null;
24
25
    /**
26
     * @var StitchApi
27
     */
28
    private $client = null;
29
30
    public function __construct()
31
    {
32
        // Use environment defines for client ID and access token by default
33
        if (defined('STITCHDATA_CLIENT_ID')) {
34
            $this->clientID = STITCHDATA_CLIENT_ID;
35
        }
36
        if (defined('STITCHDATA_ACCESS_TOKEN')) {
37
            $this->accessToken = STITCHDATA_ACCESS_TOKEN;
38
        }
39
    }
40
41
    /**
42
     * Set the client ID of your StitchData account.
43
     * @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...
44
     */
45
    public function setClientID($clientID)
46
    {
47
        $this->clientID = $clientID;
48
        return $this;
49
    }
50
51
    /**
52
     * Set the access token for the StitchData Import API
53
     * @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...
54
     */
55
    public function setAccessToken($accessToken)
56
    {
57
        $this->accessToken = $accessToken;
58
        return $this;
59
    }
60
61
    /**
62
     * Set the table name to write packages to
63
     * @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...
64
     */
65
    public function setTableName($tableName)
66
    {
67
        $this->tableName = $tableName;
68
        return $this;
69
    }
70
71
    /**
72
     * Get the client ID of your StitchData account.
73
     * @return string
74
     */
75
    public function getClientID()
76
    {
77
        return $this->clientID;
78
    }
79
80
    /**
81
     * Get the access token for the StitchData Import API
82
     * @return string
83
     */
84
    public function getAccessToken()
85
    {
86
        return $this->accessToken;
87
    }
88
89
    /**
90
     * Get the table name to write packages to
91
     * @return string
92
     */
93
    public function getTableName()
94
    {
95
        return $this->tableName;
96
    }
97
98
    /**
99
     * Set the StitchApi client instance
100
     *
101
     * @param StitchApi $client
102
     * @return $this
103
     */
104
    public function setClient(StitchApi $client)
105
    {
106
        $this->client = $client;
107
        return $this;
108
    }
109
110
    /**
111
     * Get (and/or create and set) the StitchApi client instance, allowing for lazy loading in case API arguments
112
     * want to be changed after construction.
113
     *
114
     * @return StitchApi
115
     */
116
    public function getClient()
117
    {
118
        // Allow lazy loading
119
        if (!$this->client) {
120
            $this->setClient(new StitchApi($this->getClient(), $this->getAccessToken()));
121
        }
122
        return $this->client;
123
    }
124
125
    /**
126
     * Send the given package to the StitchData API
127
     */
128
    public function sendAddon(Addon $package)
129
    {
130
        // If unconfigured, silently no-op
131
        if (!$this->getClientID() || !$this->getAccessToken() || !$this->getTableName()) {
132
            return;
133
        }
134
135
        $this->getClient()->pushRecords(
136
            $this->getTableName(),
137
            [ 'Name' ],
138
            [
139
                $this->addonToJson($package)
140
            ]
141
        );
142
    }
143
144
    public function addonToJson(Addon $package)
145
    {
146
        $tz = new DateTimeZone('UTC');
147
148
        $data = $package->toMap();
149
        unset($data['LastEdited']);
150
        unset($data['Created']);
151
        unset($data['ClassName']);
152
        unset($data['RecordClassName']);
153
154
        foreach (['Released', 'LastUpdated', 'LastBuilt'] as $field) {
155
            if ($package->$field) {
156
                $datetime = is_numeric($package->$field) ? "@" . $package->$field : $package->$field;
157
                $data[$field] = new Datetime($datetime, $tz);
158
            }
159
        }
160
161
        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...
162
            $data['RatingDetails'] = [];
163
            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...
164
                $data['RatingDetails'][] = [
165
                    'Name' => $k,
166
                    'Value' => $v,
167
                ];
168
            }
169
        }
170
171
        $data['Versions'] = [];
172
        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...
173
            $versionData = $version->toMap();
174
            unset($versionData['LastEdited']);
175
            unset($versionData['Created']);
176
            unset($versionData['ClassName']);
177
            unset($versionData['RecordClassName']);
178
            unset($versionData['AddonID']);
179
180
            if ($version->Released) {
181
                $versionData['Released'] = new Datetime($version->Released, $tz);
182
            }
183
184
            foreach ($version->CompatibleVersions() as $compatible) {
185
                $versionData['CompatibleVersions'][] = [
186
                    "Version" => $compatible->Name,
187
                    "Major" => $compatible->Major,
188
                    "Minor" => $compatible->Minor,
189
                ];
190
            }
191
192
            foreach ($version->Authors() as $author) {
193
                $authorData = $author->toMap();
194
                unset($authorData['ID']);
195
                unset($authorData['ClassName']);
196
                unset($authorData['RecordClassName']);
197
                unset($authorData['LastEdited']);
198
                unset($authorData['Created']);
199
                $versionData['Authors'][] = $authorData;
200
            }
201
202
            $data['Versions'][] = $versionData;
203
        }
204
205
        return $data;
206
    }
207
}
208