Completed
Push — develop ( 26a8c8...e71090 )
by Carsten
02:01
created

AssignUserToRetailer::__invoke()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.407

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 12
cts 17
cp 0.7059
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 5
nop 2
crap 4.407
1
<?php
2
namespace Germania\UsersRetailers;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
class AssignUserToRetailer
8
{
9
10
    /**
11
     * Database table
12
     * @var string
13
     */
14
    public $table = "users_retailers";
15
16
    /**
17
     * @var PDOStatement
18
     */
19
    public $stmt;
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    public $logger;
25
26
27
    /**
28
     * @param PDO             $pdo     PDO instance
29
     * @param LoggerInterface $logger  Optional: PSR-3 Logger
30
     * @param string          $table   Optional: Database table name
31
     */
32 2 View Code Duplication
    public function __construct(\PDO $pdo, LoggerInterface $logger = null, $table = null)
1 ignored issue
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...
33
    {
34
        // Setup
35 2
        $this->table  = $table  ?: $this->table;
36 2
        $this->logger = $logger ?: new NullLogger;
37
38
39
        // Prepare business
40 2
        $sql = "INSERT INTO {$this->table}
41
        (user_id, retailer_number)
42 2
        VALUES (:user_id, :retailer_number)";
43
44 2
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type object<PDOStatement> is incompatible with the declared type object<Germania\UsersRetailers\PDOStatement> of property $stmt.

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...
45 2
    }
46
47
48
    /**
49
     * @param  int  $user_id
50
     * @param  int  $retailer_number
51
     *
52
     * @return bool
53
     */
54 1
    public function __invoke( $user_id, $retailer_number )
55
    {
56
        try {
57 1
            $result = $this->stmt->execute([
58 1
                'user_id'         => $user_id,
59
                'retailer_number' => $retailer_number
60 1
            ]);
61 1
        } catch(\PdoException $e) {
62
            if ($e->getCode = 23000) {
0 ignored issues
show
Bug introduced by
The property getCode does not seem to exist in PDOException.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
                // Duplicate selector:
64
                // Not quite what we wanted, but useful in this case.
65
                $result = true;
66
            }
67
            else {
68
                throw $e;
69
            }
70
        }
71
72
73
        // Evaluate
74
        $loginfo = [
75 1
            'user_id'         => $user_id,
76 1
            'retailer_number' => $retailer_number,
77
            'result'          => $result
78 1
        ];
79
80 1
        if ($result):
81 1
            $this->logger->notice("Successfully assigned user to retailer number.", $loginfo);
82 1
        else:
83
            $this->logger->warning("Could not assign user to retailer number?!", $loginfo);
84
        endif;
85
86
87 1
        return $result;
88
    }
89
90
91
}
92