Completed
Push — dev ( 6a93ce...82e8fe )
by Zach
03:22
created

ImagesTableSeeder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 1
A makeProjectImages() 0 14 2
A makePageImages() 0 12 1
A moveImage() 0 6 1
A addToProject() 0 8 1
1
<?php
2
3
namespace Larafolio\database\seeds;
4
5
use App\User;
6
use Larafolio\Models\Page;
7
use Larafolio\Models\Project;
8
use Illuminate\Database\Seeder;
9
use Illuminate\Filesystem\Filesystem;
10
11
class ImagesTableSeeder extends Seeder
12
{
13
    /**
14
     * Lookup table to map images to projects.
15
     *
16
     * @var array
17
     */
18
    protected $projectLookup = [
19
        'a' => 1,
20
        'b' => 2,
21
        'c' => 3,
22
        'd' => 4,
23
        'e' => 5,
24
    ];
25
26
    /**
27
     * User instance.
28
     *
29
     * @var User
30
     */
31
    protected $user;
32
33
    /**
34
     * Run the database seeds.
35
     */
36
    public function run()
37
    {
38
        $filesystem = new Filesystem();
39
40
        $this->user = $user = User::find(1);
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
42
        $this->makeProjectImages($filesystem);
43
44
        $this->makePageImages($filesystem);
45
46
        $old = umask(0);
47
48
        chmod(storage_path('app/public/images'), 0775);
49
50
        umask($old);
51
    }
52
53
    /**
54
     * Make images for project.
55
     *
56
     * @param  \Illuminate\Filesystem\Filesystem $filesystem
57
     */
58
    protected function makeProjectImages(Filesystem $filesystem)
59
    {
60
        $images = $filesystem->allFiles(__DIR__.'/../../../tests/_data/images');
61
62
        foreach ($images as $image) {
63
            $name = $image->getFilename();
64
65
            $path = 'public/images/'.$name;
66
67
            $this->moveImage($image, $path, $filesystem);
68
69
            $this->addToProject($name, $path);
70
        }
71
    }
72
73
    /**
74
     * Make images for project.
75
     *
76
     * @param  \Illuminate\Filesystem\Filesystem $filesystem
77
     */
78
    protected function makePageImages(Filesystem $filesystem)
79
    {
80
        $image = $filesystem->get(__DIR__.'/../../../tests/_data/new.jpg');
81
82
        $path = 'public/images/pageImage.jpg';
83
84
        \Storage::put($path, $image);
1 ignored issue
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
86
        $page = Page::first();
87
88
        $this->user->addImageToModel($page, ['path' => $path]);
89
    }
90
91
    /**
92
     * Move image file to storage.
93
     *
94
     * @param \Symfony\Component\Finder\SplFileInfo $image      File info.
95
     * @param string                                $path       Path to move to.
96
     * @param \Illuminate\Filesystem\Filesystem     $filesystem
97
     */
98
    protected function moveImage($image, $path, Filesystem $filesystem)
99
    {
100
        $imageFile = $filesystem->get($image->getPathname());
101
102
        \Storage::put($path, $imageFile);
103
    }
104
105
    /**
106
     * Add image to a project.
107
     *
108
     * @param string $name Original filename.
109
     * @param string $path Path to image.
110
     */
111
    protected function addToProject($name, $path)
112
    {
113
        $key = $this->projectLookup[substr($name, 0, 1)];
114
115
        $project = Project::find($key);
116
117
        $this->user->addImageToModel($project, ['path' => $path]);
118
    }
119
}
120