DBResources::findKeyByValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?PHP
2
3
/**
4
 * AppserverIo\Resources\DBResources
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/resources
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Resources;
22
23
use AppserverIo\Lang\String;
24
use AppserverIo\Collections\ArrayList;
25
use AppserverIo\Resources\Exceptions\ResourcesKeyException;
26
27
/**
28
 * This class acts as a container resources stored in a database.
29
 *
30
 * Properties for the database connection are:
31
 *
32
 * db.connect.dsn = sqlite:/tmp/my-database.sqlite
33
 * db.connect.user =
34
 * db.connect.password =
35
 * db.sql.table = resources
36
 * db.sql.locale.column = locale
37
 * db.sql.key.column = msgKey
38
 * db.sql.val.column = val
39
 * resource.cache = true
40
 *
41
 * @author    Tim Wagner <[email protected]>
42
 * @copyright 2018 TechDivision GmbH <[email protected]>
43
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
44
 * @link      https://github.com/appserver-io/resources
45
 * @link      http://www.appserver.io
46
 */
47
class DBResources extends AbstractResources
48
{
49
50
    /**
51
     * Holds the data directory with the path to the resource files to export.
52
     *
53
     * @var \AppserverIo\Lang\String
54
     */
55
    protected $config = null;
56
57
    /**
58
     * The constructor initializes the resources with the database connection to load the resources from.
59
     *
60
     * @param \AppserverIo\Lang\String $name   Holds the logical name of the Resources to create
61
     * @param \AppserverIo\Lang\String $config Holds the optional string to the configuration
62
     */
63
    public function __construct(String $name, String $config = null)
64
    {
65
66
        // initialize the name
67
        parent::__construct($name);
68
69
        // initialize the members with the passed values
70
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config can also be of type string. However, the property $config is declared as type AppserverIo\Lang\String. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
71
    }
72
73
    /**
74
     * This method searches in the container for the resource with the key passed as parameter.
75
     *
76
     * @param string                              $name         Holds the key of the requested resource
77
     * @param \AppserverIo\Resources\SystemLocale $systemLocale Holds the SystemLocale with which to localize retrieval, or null for the default SystemLocale
78
     * @param \AppserverIo\Collections\ArrayList  $parameter    Holds an ArrayList with parameters with replacements for the placeholders in the resource string
79
     *
80
     * @return string Holds the requested resource value
81
     * @throws \AppserverIo\Resources\Exceptions\ResourcesException Is thrown if an error occurs retrieving or returning the requested content
82
     * @throws \AppserverIo\Resources\Exceptions\ResourcesKeyException Is thrown if the no value for the specified key was found, and isReturnNull() returns false
83
     * @see \AppserverIo\Resources\Interfaces\ResourcesInterface::find()
84
     */
85
    public function find($name, SystemLocale $systemLocale = null, ArrayList $parameter = null)
86
    {
87
88
        // if no system locale is passed, use the default one
89
        if ($systemLocale == null) {
90
            $systemLocale = $this->getDefaultSystemLocale();
91
        }
92
93
        // check if the property resources bundle has already been loaded
94
        if (!$this->exists($systemLocale)) {
95
            // load the resource bundle and return the value
96
            $this->add(DBResourceBundle::getBundle($this->config, $systemLocale));
97
        }
98
99
        // return the requested resource value
100
        $value = $this->get($systemLocale)->find($name, $parameter);
101
102
        // check if an exception should be thrown if the requested value is null
103
        if (($value == null) && ($this->isReturnNull() == false)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
104
            throw new ResourcesKeyException('Found no value for requested resource ' . $name);
105
        }
106
107
        // return the requested value
108
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type string which is incompatible with the documented return type AppserverIo\Lang\String.
Loading history...
109
    }
110
111
    /**
112
     * This method returns the first key found for
113
     * the passed value.
114
     *
115
     * @param string $value Holds the resource value to return the key for
116
     * @return string Holds the resource key for the passed value
117
     */
118
    public function findKeyByValue($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    public function findKeyByValue(/** @scrutinizer ignore-unused */ $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        // @TODO Still to implement
121
    }
122
123
    /**
124
     * This method returns the number of resources in the container.
125
     *
126
     * @return integer Number of resources in the container
127
     */
128
    public function count()
129
    {
130
        // @TODO Still to implement
131
    }
132
}
133