Passed
Push — master ( dcc276...a6d10d )
by David
03:59
created

Metadata::setAttribute()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 21
c 5
b 0
f 0
dl 0
loc 34
rs 6.9666
cc 12
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Vaites\ApacheTika\Metadata;
4
5
use DateTime;
6
use DateTimeZone;
7
use Exception;
8
use stdClass;
9
10
/**
11
 * Standarized metadata class with common attributes for all document types
12
 *
13
 * @author  David Martínez <[email protected]>
14
 */
15
abstract class Metadata implements MetadataInterface
16
{
17
    /**
18
     * Title
19
     *
20
     * @var string
21
     */
22
    public $title = null;
23
24
    /**
25
     * Content
26
     *
27
     * @var string
28
     */
29
    public $content = null;
30
31
    /**
32
     * MIME type
33
     *
34
     * @var string
35
     */
36
    public $mime = null;
37
38
    /**
39
     * Date created
40
     *
41
     * @var \DateTime
42
     */
43
    public $created = null;
44
45
    /**
46
     * Date updated or last modified
47
     *
48
     * @var \DateTime
49
     */
50
    public $updated = null;
51
52
    /**
53
     * RAW attributes returned by Apache Tika
54
     *
55
     * @var \stdClass
56
     */
57
    public $meta = null;
58
59
    /**
60
     * Parse Apache Tika response filling all properties
61
     *
62
     * @throws \Exception
63
     */
64
    public function __construct(stdClass $meta, string $file)
65
    {
66
        $this->meta = $meta;
67
68
        // process each meta
69
        foreach((array) $this->meta as $key => $value)
70
        {
71
            $this->setAttribute($key, $value);
72
        }
73
74
        // file name without extension if title is not detected
75
        if(empty($this->title))
76
        {
77
            $this->title = (string) preg_replace('/\..+$/', '', basename($file));
78
        }
79
80
        // use creation date as last modified if not detected
81
        if(empty($this->updated))
82
        {
83
            $this->updated = $this->created;
84
        }
85
    }
86
87
    /**
88
     * Return an instance of Metadata based on content type
89
     *
90
     * @throws \Exception
91
     */
92
    public static function make(stdClass $meta, string $file): MetadataInterface
93
    {
94
        // get content type
95
        $mime = is_array($meta->{'Content-Type'}) ? current($meta->{'Content-Type'}) : $meta->{'Content-Type'};
96
97
        // instance based on content type
98
        switch(current(explode('/', $mime)))
99
        {
100
            case 'image':
101
                $instance = new ImageMetadata($meta, $file);
102
                break;
103
104
            default:
105
                $instance = new DocumentMetadata($meta, $file);
106
        }
107
108
        return $instance;
109
    }
110
111
    /**
112
     * Sets an attribute
113
     *
114
     * @param mixed $value
115
     * @return \Vaites\ApacheTika\Metadata\MetadataInterface
116
     * @throws \Exception
117
     */
118
    public final function setAttribute(string $key, $value): MetadataInterface
119
    {
120
        $timezone = new DateTimeZone('UTC');
121
122
        switch(mb_strtolower($key))
123
        {
124
            case 'content-type':
125
                $mime = $value ? (preg_split('/;\s+/', $value) ?: []) : [];
126
127
                if(count($mime))
128
                {
129
                    $this->mime = array_shift($mime);
130
                }
131
                break;
132
133
            case 'creation-date':
134
            case 'date':
135
            case 'meta:creation-date':
136
                $value = preg_replace('/\.\d+/', 'Z', $value);
137
                $this->created = new DateTime(is_array($value) ? array_shift($value) : $value, $timezone);
138
                break;
139
140
            case 'last-modified':
141
            case 'modified':
142
                $value = preg_replace('/\.\d+/', 'Z', $value);
143
                $this->updated = new DateTime(is_array($value) ? array_shift($value) : $value, $timezone);
144
                break;
145
146
            default:
147
                $this->setSpecificAttribute($key, $value);
148
149
        }
150
151
        return $this;
152
    }
153
154
    /**
155
     * Sets an speficic attribute for the file type
156
     *
157
     * @param mixed $value
158
     * @return \Vaites\ApacheTika\Metadata\MetadataInterface
159
     */
160
    abstract protected function setSpecificAttribute(string $key, $value): MetadataInterface;
161
}
162