Metadata   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 21
eloc 45
c 8
b 1
f 0
dl 0
loc 148
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
A make() 0 17 3
C setAttribute() 0 36 14
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 'dcterms:created':
136
            case 'meta:creation-date':
137
                $value = preg_replace('/\.\d+/', 'Z', $value);
138
                $this->created = new DateTime(is_array($value) ? array_shift($value) : $value, $timezone);
139
                break;
140
141
            case 'dcterms:modified':
142
            case 'last-modified':
143
            case 'modified':
144
                $value = preg_replace('/\.\d+/', 'Z', $value);
145
                $this->updated = new DateTime(is_array($value) ? array_shift($value) : $value, $timezone);
146
                break;
147
148
            default:
149
                $this->setSpecificAttribute($key, $value);
150
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * Sets an speficic attribute for the file type
158
     *
159
     * @param mixed $value
160
     * @return \Vaites\ApacheTika\Metadata\MetadataInterface
161
     */
162
    abstract protected function setSpecificAttribute(string $key, $value): MetadataInterface;
163
}
164