Completed
Pull Request — master (#1)
by Spencer
04:00
created

AbstractScaleEngineModelFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace FloSports\ScaleEngine\Model\Factory;
3
4
use DateTime;
5
use DateTimeZone;
6
use Exception;
7
use Guzzle\Service\Resource\Model;
8
9
/**
10
 * Collects shared functionality used by all ScaleEngine models.
11
 */
12
abstract class AbstractScaleEngineModelFactory
13
{
14
    /** @type string The expected date format ScaleEngine uses. **/
15
    const SCALEENGINE_DATE_FORMAT = 'Y-m-d H:i:s';
16
17
    /** @type string The full date string ScaleEngine uses for a null date. **/
18
    const SCALEENGINE_NULL_DATE = '0000-00-00 00:00:00';
19
20
    /** @type string The timezone ScaleEngine uses for its dates. **/
21
    const SCALEENGINE_TIMEZONE = 'UTC';
22
23
    /** @type model The model class name. **/
24
    private $_model;
25
26
    /** @type array A map of keys from ScaleEngine's API to exposed model. **/
27
    private $_keyMap;
28
29
    /** @type array The data stored for the model. **/
30
    private $_data;
0 ignored issues
show
Unused Code introduced by
The property $_data is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    /**
33
     * Create the model factory for a specific type of model.
34
     *
35
     * @param string $model The full class name for the model to construct.
36
     * @param array $keyMap A map of keys from ScaleEngine's API to exposed model.
37
     */
38
    public function __construct($model, array $keyMap)
39
    {
40
        $this->_model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type string is incompatible with the declared type object<FloSports\ScaleEngine\Model\Factory\model> of property $_model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->_keyMap = $keyMap;
42
    }
43
44
    /**
45
     * Create a Ticket model using the provided data.
46
     *
47
     * The data given will be parsed and any missing keys will result in
48
     * failure.
49
     *
50
     * @param array $data The data to use to populate the model.
51
     * @return self The parsed model.
52
     */
53
    public function create(array $data)
54
    {
55
        $model = $this->_model;
56
        return $model::fromConfig($this->parseData($data), [], $this->_keyMap);
57
    }
58
59
    /**
60
     * Parse the data given into correct types and changes keys based on the
61
     * `KEY_MAP`.
62
     *
63
     * @param array $data The ticket data to parse.
64
     * @return array The data after being converted to the parsed form.
65
     */
66
    abstract public function parseData(array $data);
67
68
    /**
69
     * Convert a date from ScaleEngine's format into a PHP DateTime.
70
     *
71
     * ScaleEngine uses an all-zeros response for null dates, and this will
72
     * convert those into null itself.
73
     *
74
     * This also considers that ScaleEngine uses UTC for its timezone and makes
75
     * sure that the time returned is correct considering that behavior.
76
     *
77
     * @param string $date The date as returned from ScaleEngine.
78
     * @return DateTime The converted date/time object.
79
     */
80
    public function convertDate($date)
81
    {
82
        if ($date === static::SCALEENGINE_NULL_DATE) {
83
            return null;
84
        }
85
86
        return DateTime::createFromFormat(
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...SCALEENGINE_TIMEZONE)); of type DateTime|false adds false to the return on line 86 which is incompatible with the return type documented by FloSports\ScaleEngine\Mo...delFactory::convertDate of type DateTime|null. It seems like you forgot to handle an error condition.
Loading history...
87
            static::SCALEENGINE_DATE_FORMAT,
88
            $date,
89
            new DateTimeZone(static::SCALEENGINE_TIMEZONE)
90
        );
91
    }
92
}
93