Completed
Push — master ( a23dd5...14d659 )
by Arthur
03:38
created

CCTVController::storeSingle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 15
rs 9.4285
c 1
b 1
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
namespace BB\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Request;
7
use Illuminate\Support\Facades\Storage;
8
use Intervention\Image\Facades\Image;
9
10
class CCTVController extends Controller
11
{
12
13
    public function storeSingle()
14
    {
15
        if (Request::hasFile('image')) {
16
            $file     = Request::file('image');
17
            $fileData = Image::make($file)->encode('jpg', 80);
18
19
            $date = Carbon::now();
20
            $folderName = $date->hour . ':' . $date->minute . ':' . $date->second;
21
22
            $newFilename = \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName . '/' . str_random() . '.jpg';
23
            Storage::put($newFilename, $fileData, 'public');
24
25
            \Slack::to("#cctv")->attach(['image_url'=>'https://s3-eu-west-1.amazonaws.com/buildbrighton-bbms/' . $newFilename, 'color'=>'warning'])->send('New image');
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal #cctv does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
26
        }
27
    }
28
29
    /**
30
     * Store a newly created resource in storage.
31
     *
32
     * @return Response
33
     */
34
    public function store()
35
    {
36
        $s3 = AWS::get('s3');
37
        $s3Bucket = 'buildbrighton-bbms';
38
39
        if (Request::hasFile('image')) {
40
            $file = Request::file('image');
41
            $event = Request::get('textevent');
42
            $time = Request::get('time');
43
44
            $fileData = Image::make($file)->encode('jpg', 80);
45
46
47
            $date = Carbon::createFromFormat('YmdHis', $event);
48
            $folderName = $date->hour . ':' . $date->minute . ':' . $date->second;
49
50
            try {
51
                $newFilename = \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName . '/' . $time . '.jpg';
52
                $s3->putObject(array(
53
                    'Bucket'        => $s3Bucket,
54
                    'Key'           => $newFilename,
55
                    //'Body'          => file_get_contents($file),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
                    'Body'          => $fileData,
57
                    'ACL'           => 'public-read',
58
                    'ContentType'   => 'image/jpg',
59
                    'ServerSideEncryption' => 'AES256',
60
                ));
61
            } catch(\Exception $e) {
62
                \Log::exception($e);
63
            }
64
            //Log::debug('Image saved :https://s3-eu-west-1.amazonaws.com/buildbrighton-bbms/'.$newFilename);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
        }
66
        if (Request::get('eventend') == 'true') {
67
68
            $event = Request::get('textevent');
69
70
            $date = Carbon::createFromFormat('YmdHis', $event);
71
            $folderName = $date->hour . ':' . $date->minute . ':' . $date->second;
72
73
            $iterator = $s3->getIterator(
74
                'ListObjects',
75
                array(
76
                    'Bucket' => $s3Bucket,
77
                    'Prefix' => \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName,
78
                    //'Prefix' => 'production/camera-photos/20150410222028',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
                )
80
            );
81
82
            $images         = [];
83
            $imageDurations = [];
84
            foreach ($iterator as $object) {
85
                $images[]         = 'https://s3-eu-west-1.amazonaws.com/buildbrighton-bbms/' . $object['Key'];
86
                $imageDurations[] = 35;
87
            }
88
89
            if (count($images) <= 2) {
90
                //only two images, probably two bad frames
91
                //delete them
92
                foreach ($iterator as $object) {
93
                    Log::debug("Deleting small event image " . $object['Key']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Deleting small event image does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
94
                    $s3->deleteObject([
95
                        'Bucket'    => $s3Bucket,
96
                        'Key'       => $object['Key'],
97
                    ]);
98
                }
99
                return;
100
            }
101
102
            $gc = new GifCreator();
103
            $gc->create($images, $imageDurations, 0);
104
            $gifBinary = $gc->getGif();
105
106
            //Delete the individual frames now we have the gif
107
            foreach ($iterator as $object) {
108
                //Log::debug("Processed gif, deleting frame, ".$object['Key']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
                $s3->deleteObject([
110
                    'Bucket'    => $s3Bucket,
111
                    'Key'       => $object['Key'],
112
                ]);
113
            }
114
115
            //Save the gif
116
            $newFilename = \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName . '.gif';
117
            $s3->putObject(
118
                array(
119
                    'Bucket'               => $s3Bucket,
120
                    'Key'                  => $newFilename,
121
                    'Body'                 => $gifBinary,
122
                    'ACL'                  => 'public-read',
123
                    'ContentType'          => 'image/gif',
124
                    'ServerSideEncryption' => 'AES256',
125
                )
126
            );
127
128
129
130
131
            //Log::debug('Event Gif generated :https://s3-eu-west-1.amazonaws.com/buildbrighton-bbms/'.$newFilename);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132
133
            \Slack::to("#cctv")->attach(['image_url'=>'https://s3-eu-west-1.amazonaws.com/buildbrighton-bbms/' . $newFilename, 'color'=>'warning'])->send('Movement detected');
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal #cctv does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
134
135
        }
136
    }
137
138
}
139