Issues (56)

src/Mapper/Proxy.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Platine ORM
5
 *
6
 * Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine ORM
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Proxy.php
33
 *
34
 *  The Proxy class
35
 *
36
 *  @package    Platine\Orm\Mapper
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Orm\Mapper;
48
49
use Platine\Orm\Entity;
50
use Platine\Orm\Mapper\DataMapper;
51
use ReflectionClass;
52
use ReflectionException;
53
use ReflectionMethod;
54
use ReflectionProperty;
55
use RuntimeException;
56
57
/**
58
 * @class Proxy
59
 * @package Platine\Orm\Mapper
60
 * @template TEntity as Entity
61
 */
62
class Proxy
63
{
64
    /**
65
     * The Entity data mapper arguments property
66
     * @var ReflectionProperty
67
     */
68
    private ReflectionProperty $dataMapperArgs;
69
70
    /**
71
     * The Entity mapper() method
72
     * @var ReflectionMethod
73
     */
74
    private ReflectionMethod $mapperMethod;
75
76
    /**
77
     * Create new instance
78
     * @throws ReflectionException
79
     */
80
    private function __construct()
81
    {
82
        $reflection = new ReflectionClass(Entity::class);
83
84
        $this->dataMapperArgs = $reflection->getProperty('dataMapperArgs');
85
        $this->mapperMethod = $reflection->getMethod('mapper');
86
87
        $this->dataMapperArgs->setAccessible(true);
88
        $this->mapperMethod->setAccessible(true);
89
    }
90
91
    /**
92
     * Get the data mapper instance for the given entity
93
     * @param TEntity $entity
0 ignored issues
show
The type Platine\Orm\Mapper\TEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
     * @return DataMapper<TEntity>
95
     */
96
    public function getEntityDataMapper(Entity $entity): DataMapper
97
    {
98
        return $this->mapperMethod->invoke($entity);
99
    }
100
101
    /**
102
     * Return the columns list
103
     * @param TEntity $entity
104
     * @return array<string, mixed>
105
     */
106
    public function getEntityColumns(Entity $entity): array
107
    {
108
        $value = $this->dataMapperArgs->getValue($entity);
109
110
        if ($value !== null) {
111
            return $value[2];
112
        }
113
        //Race condition
114
        //@codeCoverageIgnoreStart
115
        return $this->getEntityDataMapper($entity)->getRawColumns();
116
        //@codeCoverageIgnoreEnd
117
    }
118
119
    /**
120
     * Return the instance
121
     * @staticvar type $proxy
122
     * @return Proxy<TEntity>
123
     * @throws RuntimeException
124
     */
125
    public static function instance(): Proxy
126
    {
127
        static $proxy = null;
128
129
        if ($proxy === null) {
130
            //Race condition
131
            //@codeCoverageIgnoreStart
132
            try {
133
                $proxy = new self();
134
            } catch (ReflectionException $exception) {
135
                throw new RuntimeException(
136
                    sprintf(
137
                        'Can not make instance of [%s] check if the entity class exists',
138
                        Proxy::class
139
                    ),
140
                    (int) $exception->getCode(),
141
                    $exception->getPrevious()
142
                );
143
            }
144
            //@codeCoverageIgnoreEnd
145
        }
146
147
        return $proxy;
148
    }
149
}
150