Completed
Push — master ( f077fb...76bf7f )
by Joschi
05:21
created

RepositoryLocator::toRepositoryUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
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\Object\Domain\Model\Uri;
38
39
use Apparat\Object\Domain\Repository\RepositoryInterface;
40
41
/**
42
 * Repository object locator
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Domain
46
 */
47
class RepositoryLocator extends Locator implements RepositoryLocatorInterface
48
{
49
    /**
50
     * Repository
51
     *
52
     * @var RepositoryInterface
53
     */
54
    protected $repository;
55
56
    /**
57
     * Repository locator constructor
58
     *
59
     * @param RepositoryInterface $repository Object repository this locator applies to
60
     * @param null|string|LocatorInterface $locator Object locator
61
     */
62 55
    public function __construct(RepositoryInterface $repository, $locator = null)
63
    {
64 55
        $this->repository = $repository;
65
66
        // If an instantiated locator (local locator, repository locator, object URL) is given
67 55
        if ($locator instanceof LocatorInterface) {
68 42
            $this->creationDate = $locator->getCreationDate();
69 42
            $this->uid = $locator->getId();
70 42
            $this->type = $locator->getObjectType();
71 42
            $this->revision = $locator->getRevision();
72 42
            return;
73
        }
74
75
        // Else: Parse as string
76 34
        parent::__construct($locator);
77 34
    }
78
79
    /**
80
     * Return the repository relative object locator with a file extension
81
     *
82
     * @param string $extension File extension
83
     * @return string Repository relative object locator with extension
84
     */
85 47
    public function withExtension($extension)
86
    {
87 47
        return $this.'.'.strtolower($extension);
88
    }
89
90
    /**
91
     * Serialize as repository URL
92
     *
93
     * @param bool $local Local URL only
94
     * @param bool $canonical Canonical URL only
95
     * @return string Repository URL
96
     */
97 5
    public function toRepositoryUrl($local = false, $canonical = false)
98
    {
99 5
        $repositoryUrl = $local ? '' : getenv('APPARAT_BASE_URL');
100 5
        $repositoryUrl .= $this->getRepository()->getUrl();
101 5
        return $repositoryUrl.$this->toUrl($canonical);
102
    }
103
104
    /**
105
     * Return the repository this locator applies to
106
     *
107
     * @return RepositoryInterface Repository
108
     */
109 51
    public function getRepository()
110
    {
111 51
        return $this->repository;
112
    }
113
}
114