Completed
Pull Request — master (#202)
by Sam
05:10 queued 03:31
created

StitchDataSender   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 157
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A setClientID() 0 5 1
A setAccessToken() 0 5 1
A setTableName() 0 5 1
A getClientID() 0 4 1
A getAccessToken() 0 4 1
A getTableName() 0 4 1
A sendAddon() 0 17 4
B addonToJson() 0 60 8
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
                $data[$field] = new Datetime($package->$field, $tz);
117
            }
118
        }
119
120
        $data['RatingDetails'] = [];
121
        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...
122
            $data['RatingDetails'][] = [
123
                'Name' => $k,
124
                'Value' => $v,
125
            ];
126
        }
127
128
        $data['Versions'] = [];
129
        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...
130
            $versionData = $version->toMap();
131
            unset($versionData['LastEdited']);
132
            unset($versionData['Created']);
133
            unset($versionData['ClassName']);
134
            unset($versionData['RecordClassName']);
135
            unset($versionData['AddonID']);
136
137
            if ($version->Released) {
138
                $versionData['Released'] = new Datetime($version->Released, $tz);
139
            }
140
141
            foreach ($version->CompatibleVersions() as $compatible) {
142
                $versionData['CompatibleVersions'][] = [
143
                    "Version" => $compatible->Name,
144
                    "Major" => $compatible->Major,
145
                    "Minor" => $compatible->Minor,
146
                ];
147
            }
148
149
            foreach ($version->Authors() as $author) {
150
                $authorData = $author->toMap();
151
                unset($authorData['ID']);
152
                unset($authorData['ClassName']);
153
                unset($authorData['RecordClassName']);
154
                unset($authorData['LastEdited']);
155
                unset($authorData['Created']);
156
                $versionData['Authors'][] = $authorData;
157
            }
158
159
            $data['Versions'][] = $versionData;
160
        }
161
162
        return $data;
163
    }
164
}
165