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

AbstractResourceReader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.2559
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