Completed
Push — master ( 7952f1...084fcc )
by Carsten
09:57 queued 03:30
created

AssignUserToRetailer::__invoke()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.5923

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 10
cts 15
cp 0.6667
rs 9.36
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4.5923
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 8 View Code Duplication
    public function __construct(\PDO $pdo, LoggerInterface $logger = null, $table = null)
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...
33
    {
34
        // Setup
35 8
        $this->table  = $table  ?: $this->table;
36 8
        $this->logger = $logger ?: new NullLogger;
37
38
39
        // Prepare business
40 8
        $sql = "INSERT INTO {$this->table}
41
        (user_id, retailer_number)
42
        VALUES (:user_id, :retailer_number)";
43
44 8
        $this->stmt = $pdo->prepare( $sql );
45 8
    }
46
47
48
    /**
49
     * @param  int  $user_id
50
     * @param  int  $retailer_number
51
     *
52
     * @return bool
53
     */
54 4
    public function __invoke( $user_id, $retailer_number )
55
    {
56
        try {
57 4
            $result = $this->stmt->execute([
58 4
                'user_id'         => $user_id,
59 4
                'retailer_number' => $retailer_number
60
            ]);
61
        } catch(\PdoException $e) {
62
            if ($e->getCode() === "23000") {
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 4
            'user_id'         => $user_id,
76 4
            'retailer_number' => $retailer_number,
77 4
            'result'          => $result
78
        ];
79
80 4
        if ($result):
81 4
            $this->logger->notice("Successfully assigned user to retailer number.", $loginfo);
82
        else:
83
            $this->logger->warning("Could not assign user to retailer number?!", $loginfo);
84
        endif;
85
86
87 4
        return $result;
88
    }
89
90
91
}
92