Completed
Push — master ( 61852c...9e945f )
by Joschi
05:39
created

ObjectFactory::createObjectRevisions()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 6
eloc 13
c 2
b 0
f 1
nc 32
nop 3
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 8.5906
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Dev
8
 * @subpackage  Apparat\Dev\Infrastructure
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\Dev\Infrastructure\Factory;
38
39
use Apparat\Dev\Infrastructure\Mutator\ArticleObjectMutator;
40
use Apparat\Dev\Infrastructure\Mutator\ObjectMutatorInterface;
41
use Apparat\Dev\Ports\Repository;
42
use Apparat\Kernel\Ports\Kernel;
43
use Apparat\Object\Domain\Model\Object\ObjectInterface;
44
use Apparat\Object\Ports\Object;
45
46
/**
47
 * Object factory
48
 *
49
 * @package Apparat\Dev
50
 * @subpackage Apparat\Dev\Infrastructure
51
 */
52
class ObjectFactory extends AbstractObjectFactory
53
{
54
    /**
55
     * Create a repository object
56
     *
57
     * @param \DateTimeInterface $creationDate Creation date
58
     * @param int $objectId Object ID
59
     * @param array $config Object configuration
60
     */
61 1
    public function create(\DateTimeInterface $creationDate, $objectId, array $config)
62
    {
63 1
        $objectTypeMethod = ucfirst($config[Repository::TYPE]);
64 1
        $objectMutator = null;
65
66
        /** @var ObjectInterface $object */
67 1
        $object = $this->{'create'.$objectTypeMethod}($creationDate, $objectMutator);
68
69
        // Create the object revisions
70 1
        $this->createObjectRevisions($object, $config, $objectMutator);
0 ignored issues
show
Documentation introduced by
$objectMutator is of type null, but the function expects a object<Apparat\Dev\Infra...ObjectMutatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
72
        // Eventually hide the object if necessary
73 1
        if (($config[Repository::HIDDEN] > 0) ? (rand(0, 100) < ($config[Repository::HIDDEN] * 100)) : false) {
74 1
            $object->delete()->persist();
75 1
        }
76 1
    }
77
78
    /**
79
     * Create the object revisions
80
     *
81
     * @param ObjectInterface $object Object
82
     * @param array $config Object configuration
83
     * @param ObjectMutatorInterface $objectMutator Object mutator
84
     */
85 1
    protected function createObjectRevisions(
86
        ObjectInterface $object,
87
        array $config,
88
        ObjectMutatorInterface $objectMutator
89
    ) {
90
        // Determine the amount of archive revisions to create
91 1
        $revisions = ($config[Repository::REVISIONS] < 0) ?
92 1
            rand(1, abs($config[Repository::REVISIONS]) + 1) : (1 + $config[Repository::REVISIONS]);
93
94
        // Determine the draft status
95 1
        $draft = (($config[Repository::DRAFTS] > 0) ? (rand(0, 100) < $config[Repository::DRAFTS] * 100) : 0);
96
97
        // Build the revision path list
98 1
        for ($revision = 1; $revision < $revisions - $draft; ++$revision) {
99 1
            $objectMutator->mutate($object)->publish()->persist();
100
        }
101 1
        if ($revision <= $revisions - $draft) {
102 1
            $objectMutator->mutate($object)->publish()->persist();
103 1
        }
104 1
        if ($draft) {
105 1
            $objectMutator->mutate($object)->persist();
106 1
        }
107 1
    }
108
109
    /**
110
     * Create an article object
111
     *
112
     * @param \DateTimeInterface $creationDate Creation date
113
     * @return ObjectInterface Article
114
     */
115 1
    protected function createArticle(\DateTimeInterface $creationDate, ObjectMutatorInterface &$objectMutator = null)
116
    {
117
        /** @var ObjectMutatorInterface $objectMutator */
118 1
        $objectMutator = Kernel::create(ArticleObjectMutator::class);
119 1
        return $objectMutator->initialize($this->repository->createObject(Object::ARTICLE, '', [], $creationDate));
120
    }
121
}
122