Base64   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
wmc 8
lcom 0
cbo 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 4 2
A fixDestination() 0 12 4
A save() 0 8 2
1
<?php
2
3
namespace Uploader\Adapters;
4
5
use Uploader\Uploader;
6
7
/**
8
 * Adapter to save a file from an url.
9
 */
10
class Base64 implements AdapterInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function check($original)
16
    {
17
        return is_string($original) && (substr($original, 0, 5) === 'data:');
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public static function fixDestination(Uploader $uploader, $original)
24
    {
25
        if (!$uploader->getFilename()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uploader->getFilename() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
26
            $uploader->setFilename(uniqid());
27
        }
28
29
        $fileData = explode(';base64,', $original, 2);
30
31
        if (!$uploader->getExtension() && preg_match('|data:\w+/(\w+)|', $fileData[0], $match)) {
32
            $uploader->setExtension($match[1]);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function save($original, $destination)
40
    {
41
        $fileData = explode(';base64,', $original, 2);
42
43
        if (!@file_put_contents($destination, base64_decode($fileData[1]))) {
44
            throw new \RuntimeException("Unable to copy base64 to '{$destination}'");
45
        }
46
    }
47
}
48