Completed
Push — master ( 2ff2f4...64e7ee )
by Aydin
02:08
created

FilteredLs::getProblem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\LearnYouPhp\Exercise;
4
5
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
6
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
7
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
8
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
/**
12
 * Class FilteredLs
13
 * @package PhpSchool\LearnYouPhp\Exercise
14
 * @author Aydin Hassan <[email protected]>
15
 */
16
class FilteredLs extends AbstractExercise implements ExerciseInterface, StdOutExerciseCheck
17
{
18
    use TemporaryDirectoryTrait;
19
    
20
    /**
21
     * @var Filesystem
22
     */
23
    private $filesystem;
24
25
    /**
26
     * @param Filesystem $filesystem
27
     */
28
    public function __construct(Filesystem $filesystem)
29
    {
30
        $this->filesystem   = $filesystem;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function getName()
37
    {
38
        return 'Filtered LS';
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getDescription()
45
    {
46
        return 'Read files in a folder and filter by a given extension';
47
    }
48
    
49
    /**
50
     * @return array
51
     */
52
    public function getArgs()
53
    {
54
        $folder = $this->getTemporaryPath();
55
56
        $files = [
57
            "learnyouphp.dat",
58
            "learnyouphp.txt",
59
            "learnyouphp.sql",
60
            "api.html",
61
            "README.md",
62
            "CHANGELOG.md",
63
            "LICENCE.md",
64
            "md",
65
            "data.json",
66
            "data.dat",
67
            "words.dat",
68
            "w00t.dat",
69
            "w00t.txt",
70
            "wrrrrongdat",
71
            "dat",
72
        ];
73
74
        $this->filesystem->mkdir($folder);
75
        array_walk($files, function ($file) use ($folder) {
76
            $this->filesystem->dumpFile(sprintf('%s/%s', $folder, $file), '');
77
        });
78
79
        $ext = '';
80
        while ($ext === '') {
81
            $index = array_rand($files);
82
            $ext = pathinfo($files[$index], PATHINFO_EXTENSION);
83
        }
84
85
        return [$folder, $ext];
86
    }
87
88
    /**
89
     * @return null
90
     */
91
    public function tearDown()
92
    {
93
        $this->filesystem->remove($this->getTemporaryPath());
94
    }
95
}
96