Passed
Push — master ( 3cb518...ba9ea6 )
by Thomas
40s
created

ServiceAbstract::latestCreatedFileIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Contracts;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Symfony\Component\Finder\SplFileInfo;
7
use Webfactor\Laravel\Generators\Commands\MakeEntity;
8
9
abstract class ServiceAbstract
10
{
11
    protected $command;
12
13
    protected $filesystem;
14
15
    protected $relativeToBasePath;
16
17
    public function __construct(MakeEntity $command)
18
    {
19
        $this->command = $command;
20
        $this->filesystem = new Filesystem();
21
    }
22
23
    protected function addLatestFileToIdeStack()
24
    {
25
        $this->command->addFile($this->latestCreatedFileIn(base_path($this->relativeToBasePath)));
26
    }
27
28
    /**
29
     * Returns the latest created File in given Path to use it for $filesToBeOpened stack
30
     *
31
     * @param string $path
32
     *
33
     * @return SplFileInfo
34
     */
35
    protected function latestCreatedFileIn(string $path): SplFileInfo
36
    {
37
        $sortedByMTime = array_sort($this->filesystem->files($path), function ($file) {
38
            return $file->getMTime();
39
        });
40
41
        return end($sortedByMTime);
42
    }
43
}
44