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

AssignUserToRetailer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 16.47 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 14
loc 85
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 3
A __invoke() 0 35 4

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
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