Issues (21)

src/PdoProfileUpdater.php (2 issues)

1
<?php
2
namespace Germania\UserProfiles;
3
4
use Germania\UserProfiles\Exceptions\UpdateProfileException;
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
8
/**
9
 * This Callable updates a user's profile.
10
 *
11
 * Example:
12
 *
13
 *     <?php
14
 *     use Germania\UserProfiles\PdoProfileUpdater;
15
 *
16
 *     $pdo    = new PDO( ... );
17
 *     $logger = new Monolog();
18
 *     $table  = 'users';
19
 *     $user   = 42;
20
 *
21
 *     $updater = new PdoProfileUpdater( $pdo, $logger, $table);
22
 *     $result = $updater( $user, [
23
 *         'first_name'   => 'John',
24
 *         'last_name'    => 'Doe',
25
 *         'display_name' => 'John Doe',
26
 *         'email'        => '[email protected]',
27
 *         'login_name'   => '[email protected]'
28
 *     ]);
29
 *     ?>
30
 *
31
 * @author  Carsten Witt <[email protected]>
32
 */
33
class PdoProfileUpdater
34
{
35
36
    /**
37
     * Database table
38
     * @var string
39
     */
40
    public $table = 'users';
41
42
    /**
43
     * @var PDOStatement
0 ignored issues
show
The type Germania\UserProfiles\PDOStatement was not found. Did you mean PDOStatement? If so, make sure to prefix the type with \.
Loading history...
44
     */
45
    public $stmt;
46
47
    /**
48
     * @var LoggerInterface
49
     */
50
    public $logger;
51
52
53
    /**
54
     * @param PDO             $pdo         PDO instance
55
     * @param LoggerInterface $logger      Optional: PSR-3 Logger
56
     * @param string          $table       Optional: Database table name
57
     */
58 15
	public function __construct( \PDO $pdo, LoggerInterface $logger = null, $table = null )
59
    {
60
        // Setup
61 15
		$this->logger = $logger ?: new NullLogger;
62 15
        $this->table  = $table  ?: $this->table;
63
64
65
        // Prepare business
66 15
        $sql = "UPDATE {$this->table} SET
67
        user_first_name   = :first_name,
68
        user_last_name    = :last_name,
69
        user_display_name = :display_name,
70
        user_email        = :email,
71
        user_login_name   = :login_name
72
        WHERE id = :id
73 3
        LIMIT 1";
74
75
        // Perform update
76 15
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type PDOStatement or boolean is incompatible with the declared type Germania\UserProfiles\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...
77
78
79 15
	}
80
81
    /**
82
     * @param  int   $user_id   User ID
83
     * @param  array $user_data User data
84
     *
85
     * @return bool  PDOStatement execution result
86
     */
87 15
    public function __invoke( $user_id, array $user_data )
88
    {
89
90
        // Setup
91
        $required_fields = [
92 15
            'first_name',
93 3
            'last_name',
94 3
            'display_name',
95 3
            'email',
96
            'login'
97 3
        ];
98
99
        // Check data
100 15
        foreach($required_fields as $rf):
101 15
            if (!array_key_exists($rf, $user_data)) :
102
103 5
                $message = "Missing field '$rf' in user data";
104 5
                $this->logger->error( $message );
105 13
                throw new UpdateProfileException( $message );
106
107
            endif;
108 3
        endforeach;
109
110
111
        // Perform
112 10
        $result = $this->stmt->execute([
113 10
            'id' => $user_id,
114 10
            'first_name'   => $user_data[ "first_name" ],
115 10
            'last_name'    => $user_data[ "last_name" ],
116 10
            'display_name' => $user_data[ "display_name" ],
117 10
            'email'        => $user_data[ "email" ],
118 10
            'login_name'   => $user_data[ "login" ]
119 2
        ]);
120
121
122
        // Evaluate
123
        $loginfo = [
124 10
            'result'       => $result,
125 10
            'display_name' => $user_data[ "display_name" ]
126 2
        ];
127
128 10
        if ($result):
129 5
            $this->logger->info("Successfully updated user profile.", $loginfo);
130 5
            return $result;
131
        endif;
132
133 5
        $this->logger->error("Could not update user profile?!", $loginfo);
134 5
        throw new UpdateProfileException;
135
    }
136
137
}
138