Completed
Push — master ( b34e65...a23dd5 )
by Arthur
07:54
created

CCTVController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 142
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B storeSingle() 0 28 3
C store() 0 103 8
1
<?php
2
3
namespace BB\Http\Controllers;
4
5
use BB\Http\Controllers\Controller;
6
7
class CCTVController extends Controller
8
{
9
10
    public function storeSingle()
11
    {
12
        $s3 = AWS::get('s3');
13
        $s3Bucket = 'buildbrighton-bbms';
14
        if (Request::hasFile('image')) {
15
            $file     = Request::file('image');
16
            $fileData = Image::make($file)->encode('jpg', 80);
17
18
            $date = Carbon::now()->format('YmdHis');
19
            $folderName = $date->hour . ':' . $date->minute . ':' . $date->second;
20
21
            try {
22
                $newFilename = \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName . '/' . str_random() . '.jpg';
23
                $s3->putObject(array(
24
                    'Bucket'        => $s3Bucket,
25
                    'Key'           => $newFilename,
26
                    'Body'          => $fileData,
27
                    'ACL'           => 'public-read',
28
                    'ContentType'   => 'image/jpg',
29
                    'ServerSideEncryption' => 'AES256',
30
                ));
31
32
                \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...
33
            } catch(\Exception $e) {
34
                \Log::exception($e);
35
            }
36
        }
37
    }
38
39
    /**
40
     * Store a newly created resource in storage.
41
     *
42
     * @return Response
43
     */
44
    public function store()
45
    {
46
        $s3 = AWS::get('s3');
47
        $s3Bucket = 'buildbrighton-bbms';
48
49
        if (Request::hasFile('image')) {
50
            $file = Request::file('image');
51
            $event = Request::get('textevent');
52
            $time = Request::get('time');
53
54
            $fileData = Image::make($file)->encode('jpg', 80);
55
56
57
            $date = Carbon::createFromFormat('YmdHis', $event);
58
            $folderName = $date->hour . ':' . $date->minute . ':' . $date->second;
59
60
            try {
61
                $newFilename = \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName . '/' . $time . '.jpg';
62
                $s3->putObject(array(
63
                    'Bucket'        => $s3Bucket,
64
                    'Key'           => $newFilename,
65
                    //'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...
66
                    'Body'          => $fileData,
67
                    'ACL'           => 'public-read',
68
                    'ContentType'   => 'image/jpg',
69
                    'ServerSideEncryption' => 'AES256',
70
                ));
71
            } catch(\Exception $e) {
72
                \Log::exception($e);
73
            }
74
            //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...
75
        }
76
        if (Request::get('eventend') == 'true') {
77
78
            $event = Request::get('textevent');
79
80
            $date = Carbon::createFromFormat('YmdHis', $event);
81
            $folderName = $date->hour . ':' . $date->minute . ':' . $date->second;
82
83
            $iterator = $s3->getIterator(
84
                'ListObjects',
85
                array(
86
                    'Bucket' => $s3Bucket,
87
                    'Prefix' => \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName,
88
                    //'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...
89
                )
90
            );
91
92
            $images         = [];
93
            $imageDurations = [];
94
            foreach ($iterator as $object) {
95
                $images[]         = 'https://s3-eu-west-1.amazonaws.com/buildbrighton-bbms/' . $object['Key'];
96
                $imageDurations[] = 35;
97
            }
98
99
            if (count($images) <= 2) {
100
                //only two images, probably two bad frames
101
                //delete them
102
                foreach ($iterator as $object) {
103
                    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...
104
                    $s3->deleteObject([
105
                        'Bucket'    => $s3Bucket,
106
                        'Key'       => $object['Key'],
107
                    ]);
108
                }
109
                return;
110
            }
111
112
            $gc = new GifCreator();
113
            $gc->create($images, $imageDurations, 0);
114
            $gifBinary = $gc->getGif();
115
116
            //Delete the individual frames now we have the gif
117
            foreach ($iterator as $object) {
118
                //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...
119
                $s3->deleteObject([
120
                    'Bucket'    => $s3Bucket,
121
                    'Key'       => $object['Key'],
122
                ]);
123
            }
124
125
            //Save the gif
126
            $newFilename = \App::environment() . '/cctv/' . $date->year . '/' . $date->month . '/' . $date->day . '/' . $folderName . '.gif';
127
            $s3->putObject(
128
                array(
129
                    'Bucket'               => $s3Bucket,
130
                    'Key'                  => $newFilename,
131
                    'Body'                 => $gifBinary,
132
                    'ACL'                  => 'public-read',
133
                    'ContentType'          => 'image/gif',
134
                    'ServerSideEncryption' => 'AES256',
135
                )
136
            );
137
138
139
140
141
            //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...
142
143
            \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...
144
145
        }
146
    }
147
148
}
149