Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.23% covered (warning)
69.23%
9 / 13
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResourceBase
69.23% covered (warning)
69.23%
9 / 13
33.33% covered (danger)
33.33%
1 / 3
11.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 release
n/a
0 / 0
n/a
0 / 0
0
 isValidType
n/a
0 / 0
n/a
0 / 0
0
 __get
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 disposeImpl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core;
6
7use TypeError;
8use PeServer\Core\DisposerBase;
9use PeServer\Core\Throws\ArgumentException;
10use PeServer\Core\Throws\NotImplementedException;
11use PeServer\Core\Throws\ObjectDisposedException;
12use PeServer\Core\Throws\ResourceInvalidException;
13
14/**
15 * `resource` 型を持ち運ぶ。
16 *
17 * 細かい処理は継承側で対応する。
18 *
19 * @template TResource
20 * @property TResource $raw 公開リソース。
21 */
22abstract class ResourceBase extends DisposerBase
23{
24    #region variable
25
26    protected string $resourceType;
27
28    #endregion
29
30    /**
31     * 生成。
32     *
33     * @param $resource 持ち運ぶリソース。
34     * @phpstan-param TResource $resource
35     */
36    public function __construct(
37        protected $resource
38    ) {
39        if (!is_resource($resource)) {
40            if (!$resource) {
41                throw new TypeError('$resource');
42            }
43        }
44
45        $this->resourceType = get_resource_type($resource);
46        if ($this->resourceType === 'Unknown') {
47            throw new ArgumentException('$resource: ' . $this->resourceType);
48        }
49
50        if (!$this->isValidType($this->resourceType)) {
51            throw new ResourceInvalidException('$resource: ' . $this->resourceType);
52        }
53    }
54
55    #region function
56
57    /**
58     * リソース型を解放する。
59     */
60    abstract protected function release(): void;
61
62    /**
63     * リソース型は自身の扱えるものか。
64     *
65     * @param string $resourceType
66     * @return bool
67     */
68    abstract protected function isValidType(string $resourceType): bool;
69
70    public function __get(string $name): mixed
71    {
72        switch ($name) {
73            case 'raw':
74                return $this->resource;
75
76            default:
77                throw new NotImplementedException($name);
78        }
79    }
80
81    #endregion
82
83    #region DisposerBase
84
85    protected function disposeImpl(): void
86    {
87        // $resourceType = get_resource_type($this->resource);
88        // if ($resourceType === 'Unknown') {
89        //     throw new ObjectDisposedException();
90        // }
91
92        $this->release();
93
94        $this->resource = null; //@phpstan-ignore-line はーいーるーのー
95    }
96
97    #endregion
98}