1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
4
|
|
|
|
5
|
|
|
use Faker\Generator; |
6
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
7
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
8
|
|
|
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
9
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
10
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
|
13
|
|
|
class ExceptionalCoding extends AbstractExercise implements |
14
|
|
|
ExerciseInterface, |
15
|
|
|
StdOutExerciseCheck, |
16
|
|
|
FunctionRequirementsExerciseCheck |
17
|
|
|
{ |
18
|
|
|
use TemporaryDirectoryTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Filesystem |
22
|
|
|
*/ |
23
|
|
|
private $filesystem; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Generator |
27
|
|
|
*/ |
28
|
|
|
private $faker; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Filesystem $filesystem |
32
|
|
|
* @param Generator $faker |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Filesystem $filesystem, Generator $faker) |
35
|
|
|
{ |
36
|
|
|
$this->filesystem = $filesystem; |
37
|
|
|
$this->faker = $faker; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getName() |
44
|
|
|
{ |
45
|
|
|
return "Exceptional Coding"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getDescription() |
52
|
|
|
{ |
53
|
|
|
return "Introduction to Exceptions"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getArgs() |
60
|
|
|
{ |
61
|
|
|
$this->filesystem->mkdir($this->getTemporaryPath()); |
62
|
|
|
|
63
|
|
|
$fileCount = rand(2, 10); |
64
|
|
|
$realFiles = rand(1, $fileCount - 1); |
65
|
|
|
|
66
|
|
|
$files = []; |
67
|
|
|
foreach (range(1, $fileCount) as $index) { |
68
|
|
|
$file = sprintf('%s/%s.txt', $this->getTemporaryPath(), $this->faker->uuid); |
69
|
|
|
if ($index <= $realFiles) { |
70
|
|
|
$this->filesystem->touch($file); |
71
|
|
|
} |
72
|
|
|
$files[] = $file; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $files; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function tearDown() |
82
|
|
|
{ |
83
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string[] |
88
|
|
|
*/ |
89
|
|
|
public function getRequiredFunctions() |
90
|
|
|
{ |
91
|
|
|
return []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string[] |
96
|
|
|
*/ |
97
|
|
|
public function getBannedFunctions() |
98
|
|
|
{ |
99
|
|
|
return ['array_filter', 'file_exists']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|