Completed
Push — master ( 6fec9d...893872 )
by Joschi
02:58
created

createObjectSelectorFromRequest()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.7972
cc 4
eloc 13
nc 8
nop 1
crap 4
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Ports
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Ports\Factory;
38
39
use Apparat\Server\Ports\Types\Selector;
40
use Psr\Http\Message\ServerRequestInterface;
41
42
/**
43
 * Repository selector factory
44
 *
45
 * @package Apparat\Server
46
 * @subpackage Apparat\Server\Ports
47
 */
48
class RepositorySelectorFactory
49
{
50
    /**
51
     * List of relevant date parts
52
     *
53
     * @var array
54
     */
55
    protected static $dateParts = null;
56
57
    /**
58
     * Create a repository selector from a server request
59
     *
60
     * @param ServerRequestInterface $serverRequest Server request
61
     * @return string Repository selector
62
     */
63 60
    public static function createObjectSelectorFromRequest(ServerRequestInterface $serverRequest)
64
    {
65 60
        $selector = [''];
66
67
        // Collect the relevant date parts
68 60
        foreach (self::getDateParts() as $datePart) {
69 60
            $selector[] = $serverRequest->getAttribute($datePart);
70 60
        }
71
72
        // Compose the hidden / ID / type part
73 60
        $hiddenIdType = $serverRequest->getAttribute(Selector::HIDDEN) ? '.' : '';
74 60
        $hiddenIdType .= $serverRequest->getAttribute(Selector::ID).'-';
75 60
        $hiddenIdType .= $serverRequest->getAttribute(Selector::TYPE);
76 60
        $selector[] = $hiddenIdType;
77
78
        // Compose the draft / revision / format part
79 60
        $draftRevFormat = $serverRequest->getAttribute(Selector::DRAFT) ? '.' : '';
80 60
        $draftRevFormat .= $serverRequest->getAttribute(Selector::ID).'-';
81 60
        $draftRevFormat .= $serverRequest->getAttribute(Selector::REVISION);
82 60
        $selector[] = $draftRevFormat;
83
84 60
        return implode('/', $selector);
85
    }
86
87
    /**
88
     * Create and return the list of relevant date parts
89
     *
90
     * @return array Date parts
91
     */
92 60
    protected static function getDateParts()
93
    {
94 60
        if (self::$dateParts === null) {
95 1
            self::$dateParts = array_slice(
96
                [
97 1
                    Selector::YEAR,
98 1
                    Selector::MONTH,
99 1
                    Selector::DAY,
100 1
                    Selector::HOUR,
101 1
                    Selector::MINUTE,
102 1
                    Selector::SECOND,
103 1
                ],
104 1
                0,
105 1
                intval(getenv('OBJECT_DATE_PRECISION'))
106 1
            );
107 1
        }
108 60
        return self::$dateParts;
109
    }
110
}
111