Aggregate   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 11.38 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 11
dl 14
loc 123
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A applyAccountWasCreated() 0 8 1
A applyMoneyDeposited() 7 7 1
A applyMoneyWithdrawn() 7 7 1
A handleCreateAccount() 0 18 2
A handleDepositMoney() 0 11 2
A handleWithdrawMoney() 0 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace RayEmitter\Example\BankAccount;
2
3
use C4tech\RayEmitter\Domain\Aggregate as AbstractAggregate;
4
use Ramsey\Uuid\Uuid;
5
6
final class Aggregate extends AbstractAggregate
7
{
8
    /**
9
     * Minimum needed to open an account.
10
     * @var int|float
11
     */
12
    const MINIMUM_TO_OPEN = 50;
13
14
    /**
15
     * Account Was Created Event Handle
16
     *
17
     * @param  AccountWasCreated $event Event Object
18
     * @return void
19
     */
20
    protected function applyAccountWasCreated(AccountWasCreated $event)
21
    {
22
        $data = $event->getPayload();
23
        $identifier = new AccountId($event->getId());
24
        $this->root = new AggregateRoot($identifier);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \RayEmitter\Example\...regateRoot($identifier) of type object<RayEmitter\Exampl...kAccount\AggregateRoot> is incompatible with the declared type object<C4tech\RayEmitter...AggregateRootInterface> of property $root.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
        $this->root->owner = $data->owner;
0 ignored issues
show
Documentation introduced by
The property $owner is declared protected in RayEmitter\Example\BankAccount\Entity. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
        $this->root->balance = $data->deposit;
0 ignored issues
show
Documentation introduced by
The property $balance is declared protected in RayEmitter\Example\BankAccount\Entity. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
    }
28
29
    /**
30
     * Money Deposited Event Handle
31
     *
32
     * @param  MoneyDeposited $event Event Object
33
     * @return void
34
     */
35 View Code Duplication
    protected function applyMoneyDeposited(MoneyDeposited $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $data = $event->getPayload();
38
        $balance = $this->root->balance->getValue();
39
        $balance += $data->deposit->getValue();
40
        $this->root->balance = new UsDollar($balance);
41
    }
42
43
    /**
44
     * Money Withdrawn Event Handle
45
     *
46
     * @param  MoneyWithdrawn $event Event Object
47
     * @return void
48
     */
49 View Code Duplication
    protected function applyMoneyWithdrawn(MoneyWithdrawn $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $data = $event->getPayload();
52
        $balance = $this->root->balance->getValue();
53
        $balance -= $data->withdrawal->getValue();
54
        $this->root->balance = new UsDollar($balance);
55
    }
56
57
    /**
58
     * Create Account Command Handle
59
     *
60
     * Processes business logic for the account creation command.
61
     * @param  CreateAccount $command Command object.
62
     * @return AccountWasCreated      Event object.
63
     */
64
    protected function handleCreateAccount(CreateAccount $command)
65
    {
66
        $data = $command->getPayload();
67
68
        $account_id = new AccountId(Uuid::uuid4()->toString());
69
        $owner = new OwnerName($data->owner);
70
        $deposit = new UsDollar($data->deposit);
71
72
        if ($deposit->getValue() < self::MINIMUM_TO_OPEN) {
73
            throw new MinimumDepositException(sprintf(
74
                'The deposit of $%s is under the required minimum of $%s',
75
                $deposit->getValue(),
76
                self::MINIMUM_TO_OPEN
77
            ));
78
        }
79
80
        return new AccountWasCreated($account_id->getValue(), compact('owner', 'deposit'));
81
    }
82
83
    /**
84
     * Deposit Money Command Handle
85
     *
86
     * Processes business logic for the deposit money command.
87
     * @param  DepositMoney $command Command object.
88
     * @return MoneyDeposited        Event object.
89
     */
90
    protected function handleDepositMoney(DepositMoney $command)
91
    {
92
        $data = $command->getPayload();
93
        $deposit = new UsDollar($data->deposit);
94
95
        if ($deposit->getValue() < 0) {
96
            throw new MinimumDepositException('You cannot deposit a negative amount.');
97
        }
98
99
        return new MoneyDeposited($command->getId(), compact('deposit'));
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<RayEmitter\Exampl...nkAccount\DepositMoney>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
    }
101
102
    /**
103
     * Withdraw Money Command Handle
104
     *
105
     * Processes business logic for the withdraw money command.
106
     * @param  WithdrawMoney $command Command object.
107
     * @return MoneyWithdrawn         Event object.
108
     */
109
    protected function handleWithdrawMoney(WithdrawMoney $command)
110
    {
111
        $data = $command->getPayload();
112
        $withdrawal = new UsDollar($data->withdrawal);
113
114
        if ($withdrawal->getValue() < 0) {
115
            throw new MinimumWithdrawalException('You cannot withdraw a negative amount.');
116
        }
117
118
        if ($withdrawal->getValue() > $this->root->balance->getValue()) {
119
            throw new InsufficientFundsException(sprintf(
120
                'Insufficient funds. You tried to withdraw $%s but only have $%s in your account.',
121
                $withdrawal->getValue(),
122
                $this->balance->getValue()
0 ignored issues
show
Documentation introduced by
The property balance does not exist on object<RayEmitter\Example\BankAccount\Aggregate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
            ));
124
        }
125
126
        return new MoneyWithdrawn($command->getId(), compact('withdrawal'));
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<RayEmitter\Exampl...kAccount\WithdrawMoney>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
    }
128
}
129