DatabaseRead   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 101
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getDescription() 0 4 1
A getArgs() 0 4 1
A seed() 0 20 3
A verify() 0 10 1
A getType() 0 4 1
A configure() 0 4 1
1
<?php
2
3
namespace PhpSchool\LearnYouPhp\Exercise;
4
5
use Faker\Generator;
6
use PDO;
7
use PhpSchool\PhpWorkshop\Check\DatabaseCheck;
8
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
9
use PhpSchool\PhpWorkshop\Event\Event;
10
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
11
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
12
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
13
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
14
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
15
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
16
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
17
use PhpSchool\PhpWorkshop\SubmissionPatch;
18
use Symfony\Component\Filesystem\Filesystem;
19
20
/**
21
 * Class DatabaseRead
22
 * @package PhpSchool\LearnYouPhp\Exercise
23
 * @author Michael Woodawrd <[email protected]>
24
 * @author Aydin Hassan <[email protected]>
25
 */
26
class DatabaseRead extends AbstractExercise implements ExerciseInterface, DatabaseExerciseCheck, CliExercise
27
{
28
29
    /**
30
     * @var Generator
31
     */
32
    private $faker;
33
34
    /**
35
     * @var array
36
     */
37
    private $randomRecord;
38
39
    /**
40
     * @param Generator $faker
41
     */
42
    public function __construct(Generator $faker)
43
    {
44
        $this->faker = $faker;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getName()
51
    {
52
        return 'Database Read';
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getDescription()
59
    {
60
        return 'Read an SQL databases contents';
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getArgs()
67
    {
68
        return [$this->randomRecord['name']];
69
    }
70
    
71
    /**
72
     * @param PDO $db
73
     * @return void
74
     */
75
    public function seed(PDO $db)
76
    {
77
        $db
78
            ->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)');
79
        $stmt = $db->prepare('INSERT INTO users (name, age, gender) VALUES (:name, :age, :gender)');
80
81
        $names = [];
82
        for ($i = 0; $i < $this->faker->numberBetween(5, 15); $i++) {
83
            $name   = $this->faker->name;
84
            $age    = rand(18, 90);
85
            $gender = rand(0, 100) % 2 ? 'male' : 'female';
86
87
            $stmt->execute([':name' => $name, ':age' => $age, ':gender' => $gender]);
88
            $id = $db->lastInsertId();
89
            $names[$id] = $name;
90
        }
91
92
        $randomId = array_rand($names);
93
        $this->randomRecord = ['id' => $randomId, 'name' => $names[$randomId]];
94
    }
95
96
    /**
97
     * @param PDO $db
98
     * @return bool
99
     */
100
    public function verify(PDO $db)
101
    {
102
        $sql = 'SELECT name FROM users WHERE id = :id';
103
        $stmt = $db->prepare($sql);
104
105
        $stmt->execute([':id' => $this->randomRecord['id']]);
106
        $result = $stmt->fetchColumn();
107
        
108
        return $result === 'David Attenborough';
109
    }
110
111
    /**
112
     * @return ExerciseType
113
     */
114
    public function getType()
115
    {
116
        return ExerciseType::CLI();
117
    }
118
119
    /**
120
     * @param ExerciseDispatcher $dispatcher
121
     */
122
    public function configure(ExerciseDispatcher $dispatcher)
123
    {
124
        $dispatcher->requireCheck(DatabaseCheck::class);
125
    }
126
}
127