Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

SimpleDateConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 62
ccs 5
cts 11
cp 0.4545
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setSubjectConfiguration() 0 4 1
A getSubjectConfiguration() 0 4 1
A getDateConverterConfiguration() 0 4 1
A convert() 0 11 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\I18n\SimpleDateConverter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Subjects\I18n;
22
23
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
24
25
/**
26
 * Simple date converter implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class SimpleDateConverter implements DateConverterInterface
35
{
36
37
    /**
38
     * The subject configuraiton instance.
39
     *
40
     * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface
41
     */
42
    private $subjectConfiguration;
43
44
    /**
45
     * Sets the subject configuration instance.
46
     *
47
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration
48
     *
49
     * @return void
50
     */
51
    public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration)
52
    {
53
        $this->subjectConfiguration = $subjectConfiguration;
54
    }
55
56
    /**
57
     * Returns the subject configuration instance.
58
     *
59
     * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration
60
     */
61
    public function getSubjectConfiguration()
62
    {
63
        return $this->subjectConfiguration;
64
    }
65
66
    /**
67
     * Returns the date converter configuration instance.
68
     *
69
     * @return \TechDivision\Import\Configuration\Subject\DateConverterConfigurationInterface The date converter configuration
70
     */
71 2
    protected function getDateConverterConfiguration()
72
    {
73 2
        return $this->getSubjectConfiguration()->getDateConverter();
74
    }
75
76
    /**
77
     * Converts the passed date into a Magento 2 compatible date format.
78
     *
79
     * @param string $date   The date to convert
80
     * @param string $format The date format to convert to
81
     *
82
     * @return string The converted date
83
     */
84 2
    public function convert($date, $format = 'Y-m-d H:i:s')
85
    {
86
87
        // create a DateTime instance from the passed value
88 2
        if ($dateTime = \DateTime::createFromFormat($this->getDateConverterConfiguration()->getSourceDateFormat(), $date)) {
89 2
            return $dateTime->format($format);
90
        }
91
92
        // return NULL, if the passed value is NOT a valid date
93
        return null;
94
    }
95
}
96