Completed
Pull Request — master (#81)
by
unknown
04:08
created

PgTimestampChronos::fromPg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
1
<?php
2
/*
3
 * This file is part of PommProject's Foundation package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Converter;
11
12
use Cake\Chronos\Chronos;
13
use Cake\Chronos\ChronosInterface;
14
use PommProject\Foundation\Session\Session;
15
use PommProject\Foundation\Exception\ConverterException;
16
17
/**
18
 * PgTimestampChronos
19
 *
20
 * Date and timestamp converter
21
 *
22
 * @package   Foundation
23
 * @copyright 2017 Grégoire HUBERT
24
 * @author    Miha Vrhovnik
25
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
26
 * @see       ConverterInterface
27
 */
28 View Code Duplication
class PgTimestampChronos implements ConverterInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
{
30
    const TS_FORMAT = 'Y-m-d H:i:s.uP';
31
32
    /**
33
     * fromPg
34
     *
35
     * @see ConverterInterface
36
     */
37
    public function fromPg($data, $type, Session $session)
38
    {
39
        $data = trim($data);
40
41
        return $data !== '' ? new Chronos($data) : null;
42
    }
43
44
    /**
45
     * toPg
46
     *
47
     * @see ConverterInterface
48
     */
49
    public function toPg($data, $type, Session $session)
50
    {
51
        return
52
            $data !== null
53
            ? sprintf("%s '%s'", $type, $this->checkData($data)->format(static::TS_FORMAT))
54
            : sprintf("NULL::%s", $type)
55
            ;
56
    }
57
58
    /**
59
     * toPgStandardFormat
60
     *
61
     * @see ConverterInterface
62
     */
63
    public function toPgStandardFormat($data, $type, Session $session)
64
    {
65
        return
66
            $data !== null
67
            ? $this->checkData($data)->format(static::TS_FORMAT)
68
            : null
69
            ;
70
    }
71
72
    /**
73
     * checkData
74
     *
75
     * Ensure a ChronosInterface instance.
76
     *
77
     * @access protected
78
     * @param  mixed $data
79
     * @throws ConverterException
80
     * @return ChronosInterface
81
     */
82
    protected function checkData($data)
83
    {
84
        if (!$data instanceof ChronosInterface) {
85
            try {
86
                $data = new Chronos($data);
87
            } catch (\Exception $e) {
88
                throw new ConverterException(
89
                    sprintf(
90
                        "Cannot convert data from invalid datetime representation '%s'.",
91
                        $data
92
                    ),
93
                    null,
94
                    $e
95
                );
96
            }
97
        }
98
99
        return $data;
100
    }
101
}
102