Completed
Push — master ( f12f85...ac41f4 )
by Hannes
02:45
created

AbstractResourceReader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 57.89%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 51
ccs 11
cts 19
cp 0.5789
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A __destruct() 0 4 1
A setResource() 0 10 2
A closeResource() 0 6 2
readHeader() 0 1 ?
getElevationFor() 0 1 ?
1
<?php
2
3
/*
4
 * This file is part of the Runalyze DEM Reader.
5
 *
6
 * (c) RUNALYZE <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Runalyze\DEM\Provider;
13
14
abstract class AbstractResourceReader implements ResourceReaderInterface
15
{
16
    /** @var resource|bool */
17
    protected $FileResource = false;
18
19
    /**
20
     * @param resource|bool $resource
21
     */
22 8
    public function __construct($resource = false)
23
    {
24 8
        if (false !== $resource) {
25
            $this->setResource($resource);
1 ignored issue
show
Bug introduced by
It seems like $resource defined by parameter $resource on line 22 can also be of type boolean; however, Runalyze\DEM\Provider\Ab...ceReader::setResource() does only seem to accept resource, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
        }
27 8
    }
28
29
    public function __destruct()
30
    {
31
        $this->closeResource();
32
    }
33
34
    /**
35
     * @param  resource          $resource
36
     * @throws \RuntimeException
37
     */
38 6
    public function setResource($resource)
39
    {
40 6
        $this->closeResource();
41
42 6
        $this->FileResource = $resource;
43
44 6
        if (false === $this->FileResource) {
45
            throw new \RuntimeException('Provider file "'.$filename.'"" can\'t be opened for reading.');
46
        }
47 6
    }
48
49 6
    protected function closeResource()
50
    {
51 6
        if (is_resource($this->FileResource)) {
52
            fclose($this->FileResource);
53
        }
54 6
    }
55
56
    abstract public function readHeader();
57
58
    /**
59
     * @param  int      $row
60
     * @param  int      $col
61
     * @return int|bool elevation [m] can be false if nothing retrieved or value is unknown
62
     */
63
    abstract public function getElevationFor($row, $col);
64
}
65