CreateMonthBillCommandHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
require_once __DIR__ . '/../query/MonthlyBillableQueryHandler.php';
4
require_once __DIR__ . '/../query/MonthlyBillableQuery.php';
5
require_once __DIR__ . '/CreateReceiverMonthBillCommandHandler.php';
6
require_once __DIR__ . '/CreateReceiverMonthBillCommand.php';
7
require_once __DIR__ . '/CreateMonthBillCommand.php';
8
require_once __DIR__ . '/CommandHandlerInterface.php';
9
require_once __DIR__ . '/CommandInterface.php';
10
11
/**
12
 * @author Laurent De Coninck <[email protected]>
13
 */
14
class CreateMonthBillCommandHandler implements CommandHandlerInterface
15
{
16
    /**
17
     * @var DoliDB
18
     */
19
    private $db;
20
21
    /**
22
     * @var stdClass
23
     */
24
    private $conf;
25
26
    /**
27
     * @var User
28
     */
29
    private $user;
30
31
    /**
32
     * @var Translate
33
     */
34
    private $langs;
35
36
    /**
37
     * @var MonthlyBillableQueryHandler
38
     */
39
    private $queryHandler;
40
41
    /**
42
     * @var CreateReceiverMonthBillCommandHandler
43
     */
44
    private $receiverMonthBillCommandHandler;
45
46
    /**
47
     * @param DoliDB    $db
48
     * @param stdClass  $conf
49
     * @param User      $user
50
     * @param Translate $langs
51
     */
52
    public function __construct($db, $conf, $user, $langs)
53
    {
54
        $this->db = $db;
55
        $this->conf = $conf;
56
        $this->user = $user;
57
        $this->langs = $langs;
58
        $this->queryHandler = new MonthlyBillableQueryHandler($db, $conf);
59
        $this->receiverMonthBillCommandHandler = new CreateReceiverMonthBillCommandHandler($db, $conf, $user, $langs);
60
    }
61
62
    /**
63
     * @param CommandInterface|CreateMonthBillCommand $command
64
     *
65
     * @throws Exception
66
     */
67
    public function handle(CommandInterface $command)
68
    {
69
        $queryResult = $this->queryHandler->__invoke(new MonthlyBillableQuery(
70
                $command->getMonth(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getMonth() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
71
                $command->getYear())
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getYear() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
72
        );
73
74
        foreach ($queryResult->getFlights() as $currentReceiver) {
75
            $this->db->begin();
76
77
            try {
78
                $this->createBill($command, $currentReceiver);
79
                $this->db->commit();
80
            } catch (\Exception $e) {
81
                $this->db->rollback($e->getTraceAsString());
82
                throw $e;
83
            }
84
85
        }
86
87
    }
88
89
    /**
90
     * @param CreateMonthBillCommand|CommandInterface $command
91
     * @param MonthlyFlightBill                       $currentReceiver
92
     */
93
    private function createBill($command, $currentReceiver)
94
    {
95
        $receiverCommand = new CreateReceiverMonthBillCommand(
96
            $currentReceiver,
97
            $command->getBillType(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getBillType() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
98
            $command->getPublicNote(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getPublicNote() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
99
            $command->getPrivateNote(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getPrivateNote() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
100
            $command->getYear(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getYear() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
101
            $command->getMonth()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getMonth() does only exist in the following implementations of said interface: CreateMonthBillCommand, CreateReceiverMonthBillCommand.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
        );
103
104
        $this->receiverMonthBillCommandHandler->handle($receiverCommand);
105
    }
106
}