Completed
Push — master ( 3e1f6e...114870 )
by Žilvinas
03:03
created

DocumentTypeGuesser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 47
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A setGuessers() 0 4 1
A guess() 0 18 4
A getExtension() 0 5 1
1
<?php
2
3
namespace Isign;
4
5
use Isign\DocumentTypeGuesser;
6
use Isign\Exception\NotSupportedDocumentType;
7
8
/**
9
 * Guesses document type
10
 */
11
class DocumentTypeGuesser
12
{
13
    private $guessers;
14
15 2
    public function __construct()
16
    {
17 2
        $this->guessers = [
18
            // new DocumentTypeGuesser\Pdflt(), // Pdflt must be loaded before PDF
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
19 2
            new DocumentTypeGuesser\Pdf(),
20 2
            new DocumentTypeGuesser\Adoc(),
21 2
            new DocumentTypeGuesser\Bdoc(),
22 2
            new DocumentTypeGuesser\Ddoc(),
23 2
            new DocumentTypeGuesser\Edoc(),
24 2
            new DocumentTypeGuesser\Mdoc(),
25
        ];
26 2
    }
27
28 1
    public function setGuessers(array $guessers)
29
    {
30 1
        $this->guessers = $guessers;
31 1
    }
32
33 2
    public function guess($path)
34
    {
35 2
        $extension = $this->getExtension($path);
36 2
        $content = file_get_contents($path);
37
38 2
        $type = null;
39 2
        foreach ($this->guessers as $guesser) {
40 1
            if ($type = $guesser->guess($content, $extension)) {
41 1
                break;
42
            }
43 2
        }
44
45 2
        if ($type === null) {
46 1
            throw new NotSupportedDocumentType('Document type is not supported');
47
        }
48
49 1
        return $type;
50
    }
51
52 2
    private function getExtension($path)
53
    {
54 2
        $parts = explode('.', strtolower(pathinfo($path, PATHINFO_EXTENSION)));
55 2
        return trim(end($parts));
56
    }
57
}
58