Completed
Push — master ( 18f749...9fc766 )
by Gordon
17:08 queued 02:10
created

ImageMetaDataExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.41%
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 112
ccs 66
cts 73
cp 0.9041
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C processExifData() 0 76 7
A onAfterWrite() 0 8 2
A requireDefaultRecords() 0 10 2
1
<?php
2
3
class ImageMetaDataExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
        'ExifRead' => 'Boolean',
7
        'Aperture' => 'Varchar',
8
        'ShutterSpeed' => 'Varchar',
9
        'TakenAt' => 'Datetime',
10
        'ISO' => 'Int',
11
        'Orientation' => 'Int'
12
    );
13
14
15
    public static $defaults = array('ExifRead' => false);
16
17 15
    public function processExifData()
18
    {
19 15
        $image    = $this->owner->Image();
20 15
        $filename = BASE_PATH . '/' . $image->Filename;
21
22
        // when the image is first saved, the file will still be a temp file
23 15
        if ($image->exists()) {
24
            try {
25 2
                $exif = exif_read_data($filename, null, true);
26 2
                $aperture = $exif['COMPUTED']['ApertureFNumber'];
27 2
                $aperture = str_replace('f/', '', $aperture);
28 2
                $this->owner->Aperture = $aperture;
29
30 2
                $shutterspeed = '';
31 2
                if (isset($exif['ExposureTime'])) {
32
                    $shutterspeed = $exif['ExposureTime'];
33
                } else {
34 2
                    $shutterspeed = $exif['EXIF']['ExposureTime'];
35
                }
36
37 2
                $this->owner->ShutterSpeed = $shutterspeed;
38 2
                if (isset($exif['DateTimeOriginal'])) {
39
                    $this->owner->TakenAt = $exif['DateTimeOriginal'];
40
                } else {
41 2
                    $this->owner->TakenAt = $exif['EXIF']['DateTimeOriginal'];
42
                }
43
44 2
                $iso = '';
45 2
                if (isset($exif['ISOSpeedRatings'])) {
46
                    $iso = $exif['ISOSpeedRatings'];
47
                } else {
48 2
                    $iso = $exif['EXIF']['ISOSpeedRatings'];
49
                }
50
51 2
                $this->owner->ISO = $iso;
52
53 2
                if (isset($exif['GPS'])) {
54 2
                    $gps      = $exif['GPS'];
55 2
                    $latarray = $gps['GPSLatitude'];
56 2
                    $degrees  = $latarray[0];
57 2
                    $parts    = explode('/', $degrees);
58 2
                    $degrees  = $parts[0] / $parts[1];
59 2
                    $minutes  = $latarray[1];
60 2
                    $parts    = explode('/', $minutes);
61 2
                    $minutes  = $parts[0] / $parts[1];
62 2
                    $seconds  = $latarray[2];
63 2
                    $parts    = explode('/', $seconds);
64 2
                    $seconds  = $parts[0] / $parts[1];
65 2
                    $latitude = $degrees + $minutes / 60 + $seconds / 3600;
66 2
                    $lonarray = $gps['GPSLongitude'];
67 2
                    $degrees  = $lonarray[0];
68 2
                    $parts    = explode('/', $degrees);
69 2
                    $degrees  = $parts[0] / $parts[1];
70 2
                    $minutes  = $lonarray[1];
71 2
                    $parts    = explode('/', $minutes);
72 2
                    $minutes  = $parts[0] / $parts[1];
73 2
                    $seconds  = $lonarray[2];
74 2
                    $parts    = explode('/', $seconds);
75 2
                    $seconds  = $parts[0] / $parts[1];
76
77 2
                    $longitude        = $degrees + $minutes / 60 + $seconds / 3600;
78 2
                    $this->owner->Lat = $latitude;
79 2
                    $this->owner->Lon = $longitude;
80 2
                }
81
82 2
                $image = $this->owner->Image();
0 ignored issues
show
Unused Code introduced by
$image 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...
83
84 2
                $this->owner->ExifRead = true;
85 2
                $this->owner->Orientation = $this->owner->Image()->getOrientation();
86 2
                $this->owner->write();
87
            }
88 2
            catch (Exception $e) {
89
                error_log($e->getMessage());
90
            }
91 2
        }
92 15
    }
93
94 15
    public function onAfterWrite()
95
    {
96 15
        parent::onAfterWrite();
97
98 15
        if (!($this->owner->ExifRead)) {
99 15
            $this->processExifData();
100 15
        }
101 15
    }
102
103 1
    public function requireDefaultRecords()
104
    {
105 1
        parent::requireDefaultRecords();
106 1
        $imagesToProcess = GalleryImage::get()->filter('ExifRead', 0);
107 1
        foreach ($imagesToProcess as $image) {
108 1
            $image->processExifData();
109 1
        }
110
111 1
        DB::alteration_message('Updated image metadata where EXIF has not been ' . 'processed', 'changed');
112 1
    }
113
114
}
115