DependencyHeaven::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\LearnYouPhp\Exercise;
4
5
use Faker\Generator;
6
use PhpSchool\PhpWorkshop\Check\ComposerCheck;
7
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
8
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
9
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
10
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11
use PhpSchool\PhpWorkshop\ExerciseCheck\CgiOutputExerciseCheck;
12
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
13
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
14
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
15
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
16
use Zend\Diactoros\Request;
17
18
/**
19
 * Class DependencyHeaven
20
 * @package PhpSchool\LearnYouPhp\Exercise
21
 * @author Michael Woodward <[email protected]>
22
 */
23
class DependencyHeaven extends AbstractExercise implements
24
    ExerciseInterface,
25
    CgiExercise,
26
    ComposerExerciseCheck
27
{
28
    /**
29
     * @var Generator
30
     */
31
    private $faker;
32
33
    /**
34
     * @param Generator $faker
35
     */
36
    public function __construct(Generator $faker)
37
    {
38
        $this->faker = $faker;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getName()
45
    {
46
        return 'Dependency Heaven';
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getDescription()
53
    {
54
        return 'An introduction to Composer dependency management';
55
    }
56
57
    /**
58
     * @return SolutionInterface
59
     */
60
    public function getSolution()
61
    {
62
        return DirectorySolution::fromDirectory(__DIR__ . '/../../exercises/dependency-heaven/solution');
63
    }
64
65
    /**
66
     * @return RequestInterface[]
67
     */
68
    public function getRequests()
69
    {
70
        $requests = [];
71
72
        for ($i = 0; $i < rand(2, 5); $i++) {
73
            $requests[] = $this->newApiRequest('/reverse');
74
            $requests[] = $this->newApiRequest('/swapcase');
75
            $requests[] = $this->newApiRequest('/titleize');
76
        }
77
78
        return $requests;
79
    }
80
81
    /**
82
     * @param string $endpoint
83
     * @return RequestInterface
84
     */
85
    private function newApiRequest($endpoint)
86
    {
87
        $request = (new Request($endpoint))
88
            ->withMethod('POST')
89
            ->withHeader('Content-Type', 'application/x-www-form-urlencoded');
90
91
        $request->getBody()->write(
92
            http_build_query(['data' => $this->faker->sentence(rand(3, 6))])
93
        );
94
95
        return $request;
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function getRequiredPackages()
102
    {
103
        return [
104
            'klein/klein',
105
            'danielstjules/stringy'
106
        ];
107
    }
108
109
    /**
110
     * @return ExerciseType
111
     */
112
    public function getType()
113
    {
114
        return ExerciseType::CGI();
115
    }
116
117
    /**
118
     * @param ExerciseDispatcher $dispatcher
119
     */
120
    public function configure(ExerciseDispatcher $dispatcher)
121
    {
122
        $dispatcher->requireCheck(ComposerCheck::class);
123
    }
124
}
125