GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

EventSourcingRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 7
dl 0
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEventStore() 0 4 1
A load() 0 20 4
A save() 0 3 1
1
<?php
2
/*
3
 * Copyright (c) 2012-2014 Janos Szurovecz
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6
 * this software and associated documentation files (the "Software"), to deal in
7
 * the Software without restriction, including without limitation the rights to
8
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is furnished to do
10
 * so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in all
13
 * copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
22
 */
23
24
namespace predaddy\domain\eventsourcing;
25
26
use InvalidArgumentException;
27
use precore\lang\Object;
28
use precore\lang\ObjectClass;
29
use precore\util\Preconditions;
30
use predaddy\domain\AggregateId;
31
use predaddy\domain\AggregateRoot;
32
use predaddy\domain\EventStore;
33
use predaddy\domain\Repository;
34
35
/**
36
 * Should be used for event sourcing.
37
 *
38
 * @author Janos Szurovecz <[email protected]>
39
 */
40
class EventSourcingRepository extends Object implements Repository
41
{
42
    /**
43
     * @var EventStore
44
     */
45
    private $eventStore;
46
47
    /**
48
     * @param EventStore $eventStore
49
     */
50
    public function __construct(EventStore $eventStore)
51
    {
52
        $this->eventStore = $eventStore;
53
    }
54
55
    /**
56
     * @return EventStore
57
     */
58
    public function getEventStore()
59
    {
60
        return $this->eventStore;
61
    }
62
63
    /**
64
     * Initializes the stored aggregate with its events persisted in event store.
65
     *
66
     * @param AggregateId $aggregateId
67
     * @return EventSourcedAggregateRoot
68
     * @throws InvalidArgumentException If the $aggregateId is invalid
69
     */
70
    public function load(AggregateId $aggregateId)
71
    {
72
        $aggregateRootClass = ObjectClass::forName($aggregateId->aggregateClass());
73
        $aggregate = null;
74
        $stateHash = null;
75
        if ($this->eventStore instanceof SnapshotEventStore) {
76
            $aggregate = $this->eventStore->loadSnapshot($aggregateId);
77
            $stateHash = $aggregate === null
78
                ? null
79
                : $aggregate->stateHash();
80
        }
81
        $events = $this->eventStore->getEventsFor($aggregateId, $stateHash);
82
        if ($aggregate === null) {
83
            Preconditions::checkArgument($events->valid(), 'Aggregate with ID [%s] does not exist', $aggregateId);
0 ignored issues
show
Bug introduced by
The method valid does only exist in Iterator, but not in Countable.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
            $aggregate = $aggregateRootClass->newInstanceWithoutConstructor();
85
        }
86
        $aggregateRootClass->cast($aggregate);
87
        $aggregate->loadFromHistory($events);
88
        return $aggregate;
89
    }
90
91
    /**
92
     * Persisting the given $aggregateRoot.
93
     *
94
     * @param AggregateRoot $aggregateRoot
95
     */
96
    public function save(AggregateRoot $aggregateRoot)
97
    {
98
    }
99
}
100