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

AbstractScaleEngineModelFactory::create()   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 1
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 string 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
    /**
29
     * Create the model factory for a specific type of model.
30
     *
31
     * @param string $model The full class name for the model to construct.
32
     * @param array $keyMap A map of keys from ScaleEngine's API to exposed model.
33
     */
34
    public function __construct($model, array $keyMap)
35
    {
36
        $this->_model = $model;
37
        $this->_keyMap = $keyMap;
38
    }
39
40
    /**
41
     * Create a Ticket model using the provided data.
42
     *
43
     * The data given will be parsed and any missing keys will result in
44
     * failure.
45
     *
46
     * @param array $data The data to use to populate the model.
47
     * @return \Guzzle\Service\Resource\Model The parsed model.
48
     */
49
    public function create(array $data)
50
    {
51
        $model = $this->_model;
52
        return $model::fromConfig($this->parseData($data), [], $this->_keyMap);
53
    }
54
55
    /**
56
     * Parse the data given into correct types and changes keys based on the
57
     * key map.
58
     *
59
     * @param array $data The ticket data to parse.
60
     * @return array The data after being converted to the parsed form.
61
     */
62
    abstract public function parseData(array $data);
63
64
    /**
65
     * Convert a date from ScaleEngine's format into a PHP DateTime.
66
     *
67
     * ScaleEngine uses an all-zeros response for null dates, and this will
68
     * convert those into null itself.
69
     *
70
     * This also considers that ScaleEngine uses UTC for its timezone and makes
71
     * sure that the time returned is correct considering that behavior.
72
     *
73
     * @param string $date The date as returned from ScaleEngine.
74
     * @return DateTime The converted date/time object.
75
     */
76
    public function convertDate($date)
77
    {
78
        if ($date === static::SCALEENGINE_NULL_DATE) {
79
            return null;
80
        }
81
82
        $dateTime = DateTime::createFromFormat(
83
            static::SCALEENGINE_DATE_FORMAT,
84
            $date,
85
            new DateTimeZone(static::SCALEENGINE_TIMEZONE)
86
        );
87
88
        return $dateTime ?: null;
89
    }
90
}
91