Completed
Push — master ( 2689ec...25953d )
by Joschi
02:46
created

AuthorFactory::createFromString()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 28
ccs 7
cts 11
cp 0.6364
rs 8.5806
cc 4
eloc 13
nc 7
nop 2
crap 4.7691
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)
0 ignored issues
show
Coding Style introduced by
Spaces must be used for alignment; tabs are not allowed
Loading history...
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\Factory;
38
39
use Apparat\Object\Domain\Model\Author\ApparatAuthor;
40
use Apparat\Object\Domain\Model\Author\AuthorInterface;
41
use Apparat\Object\Domain\Model\Author\GenericAuthor;
42
use Apparat\Object\Domain\Model\Author\InvalidArgumentException;
43
use Apparat\Object\Domain\Model\Author\InvalidAuthor;
44
use Apparat\Object\Domain\Model\Path\ApparatInvalidArgumentException;
45
use Apparat\Object\Domain\Model\Path\ApparatUrl;
46
use Apparat\Object\Domain\Repository\RepositoryInterface;
47
48
/**
49
 * Author factory
50
 *
51
 * @package Apparat\Object
52
 * @subpackage Apparat\Object\Domain
53
 */
54
class AuthorFactory
55
{
56
    /**
57
     * Parse and instantiate an author serialization
58
     *
59
     * @param string $author Author serialization
60
     * @param RepositoryInterface $contextRepository Context repository
61
     * @return AuthorInterface Object author
62
     * @throws InvalidArgumentException If the author format is invalid
63
     */
64 1
    public static function createFromString($author, RepositoryInterface $contextRepository = null)
65
    {
66
        // Try to instantiate an apparat object based author
67
        try {
68 1
            $apparatUrl = new ApparatUrl($author, true, $contextRepository);
69 1
            return new ApparatAuthor($apparatUrl);
70
71
            // If there's an apparat URL problem
72 1
        } catch (ApparatInvalidArgumentException $e) {
73 1
            return new InvalidAuthor($author, $e);
74
75
            // Proceed on other errors
76 1
        } catch (\Apparat\Object\Domain\Model\Path\InvalidArgumentException $e) {
77
        }
78
79
        // Try to instantiate a generic author
80
        try {
81 1
            return GenericAuthor::unserialize($author);
82
83
            // Proceed on errors
84
        } catch (InvalidArgumentException $e) {
85
        }
86
87
        throw new InvalidArgumentException(
88
            sprintf('Invalid author format "%s"', $author),
89
            InvalidArgumentException::INVALID_AUTHOR_FORMAT
90
        );
91
    }
92
}
93