Completed
Push — development ( 68678c...9250e3 )
by Romain
03:12
created

ObjectService::getObjectProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Configuration Object project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\ConfigurationObject\Core\Service;
15
16
use Romm\ConfigurationObject\Exceptions\SilentExceptionInterface;
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
19
20
class ObjectService implements SingletonInterface
21
{
22
    /**
23
     * This function will try to get a given property for a given object.
24
     *
25
     * Its particularity is that if the getter method for this property is used,
26
     * the method may throw an exception that implements the interface
27
     * `SilentExceptionInterface`. In that case, the exception is catch and
28
     * `null` is returned.
29
     *
30
     * This allows more flexibility for the developer, who may still throw
31
     * exceptions in getter methods for implementation concerns, but these
32
     * exceptions wont block Configuration Object API processing.
33
     *
34
     * @see \Romm\ConfigurationObject\Exceptions\SilentExceptionInterface
35
     * @see \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty()
36
     *
37
     * @param object $object
38
     * @param string $property
39
     * @return mixed
40
     * @throws \Exception
41
     */
42
    public function getObjectProperty($object, $property)
43
    {
44
        $result = null;
45
46
        try {
47
            $result = ObjectAccess::getProperty($object, $property);
48
        } catch (\Exception $exception) {
49
            if (false === $exception instanceof SilentExceptionInterface) {
50
                throw $exception;
51
            }
52
        }
53
54
        return $result;
55
    }
56
}
57