Completed
Pull Request — master (#1)
by Spencer
02:07
created

AbstractScaleEngineModelFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 5 1
parseData() 0 1 ?
A convertDate() 0 12 2
1
<?php
2
namespace FloSports\ScaleEngine\Model\Factory;
3
4
use DateTime;
5
use DateTimeZone;
6
use Guzzle\Service\Resource\Model;
7
8
/**
9
 * Collects shared functionality used by all ScaleEngine models.
10
 */
11
abstract class AbstractScaleEngineModelFactory
12
{
13
    /** @type string The expected date format ScaleEngine uses. **/
14
    const SCALEENGINE_DATE_FORMAT = 'Y-m-d H:i:s';
15
16
    /** @type string The full date string ScaleEngine uses for a null date. **/
17
    const SCALEENGINE_NULL_DATE = '0000-00-00 00:00:00';
18
19
    /** @type string The timezone ScaleEngine uses for its dates. **/
20
    const SCALEENGINE_TIMEZONE = 'UTC';
21
22
    /** @type model The model class name. **/
23
    private $_model;
24
25
    /** @type array A map of keys from ScaleEngine's API to exposed model. **/
26
    private $_keyMap;
27
28
    /** @type array The data stored for the model. **/
29
    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...
30
31
    /**
32
     * Create the model factory for a specific type of model.
33
     *
34
     * @param string $model The full class name for the model to construct.
35
     * @param array $keyMap A map of keys from ScaleEngine's API to exposed model.
36
     */
37
    public function __construct($model, array $keyMap)
38
    {
39
        $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...
40
        $this->_keyMap = $keyMap;
41
    }
42
43
    /**
44
     * Create a Ticket model using the provided data.
45
     *
46
     * The data given will be parsed and any missing keys will result in
47
     * failure.
48
     *
49
     * @param array $data The data to use to populate the model.
50
     * @return \Guzzle\Service\Resource\Model The parsed model.
51
     */
52
    public function create(array $data)
53
    {
54
        $model = $this->_model;
55
        return $model::fromConfig($this->parseData($data), [], $this->_keyMap);
56
    }
57
58
    /**
59
     * Parse the data given into correct types and changes keys based on the
60
     * `KEY_MAP`.
61
     *
62
     * @param array $data The ticket data to parse.
63
     * @return array The data after being converted to the parsed form.
64
     */
65
    abstract public function parseData(array $data);
66
67
    /**
68
     * Convert a date from ScaleEngine's format into a PHP DateTime.
69
     *
70
     * ScaleEngine uses an all-zeros response for null dates, and this will
71
     * convert those into null itself.
72
     *
73
     * This also considers that ScaleEngine uses UTC for its timezone and makes
74
     * sure that the time returned is correct considering that behavior.
75
     *
76
     * @param string $date The date as returned from ScaleEngine.
77
     * @return DateTime The converted date/time object.
78
     */
79
    public function convertDate($date)
80
    {
81
        if ($date === static::SCALEENGINE_NULL_DATE) {
82
            return null;
83
        }
84
85
        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 85 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...
86
            static::SCALEENGINE_DATE_FORMAT,
87
            $date,
88
            new DateTimeZone(static::SCALEENGINE_TIMEZONE)
89
        );
90
    }
91
}
92