Completed
Push — master ( 4c707a...1e14b3 )
by Zhmayev
01:23
created

Modules::getTypedID()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 2
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
* Vtiger Web Services PHP Client Library
4
*
5
* The MIT License (MIT)
6
*
7
* Copyright (c) 2015, Zhmayev Yaroslav <[email protected]>
8
*
9
* Permission is hereby granted, free of charge, to any person obtaining a copy
10
* of this software and associated documentation files (the "Software"), to deal
11
* in the Software without restriction, including without limitation the rights
12
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
* copies of the Software, and to permit persons to whom the Software is
14
* furnished to do so, subject to the following conditions:
15
*
16
* The above copyright notice and this permission notice shall be included in
17
* all copies or substantial portions of the Software.
18
*
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
* THE SOFTWARE.
26
*
27
* @author    Zhmayev Yaroslav <[email protected]>
28
* @copyright 2015-2016 Zhmayev Yaroslav
29
* @license   The MIT License (MIT)
30
*/
31
32
namespace Salaros\Vtiger\VTWSCLib;
33
34
use Salaros\Vtiger\VTWSCLib\WSClient;
35
36
/**
37
* Vtiger Web Services PHP Client Session class
38
*
39
* Class Modules
40
* @package Salaros\Vtiger\VTWSCLib
41
*/
42
class Modules
43
{
44
    private $wsClient = null;
45
46
    /**
47
     * Class constructor
48
     * @param string $wsClient  Parent WSClient instance
49
     */
50
    public function __construct($wsClient)
51
    {
52
        $this->wsClient = $wsClient;
53
    }
54
55
    /**
56
     * Lists all the Vtiger entity types available through the API
57
     * @access public
58
     * @return array List of entity types
59
     */
60
    public function getAll()
61
    {
62
        $result = $this->wsClient->invokeOperation('listtypes', [], 'GET');
0 ignored issues
show
Bug introduced by
The method invokeOperation cannot be called on $this->wsClient (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63
        $modules = $result['types'];
64
65
        $result = array();
66
        foreach ($modules as $moduleName) {
67
            $result[$moduleName] = ['name' => $moduleName];
68
        }
69
        return $result;
70
    }
71
72
    /**
73
     * Get the type information about a given VTiger entity type.
74
     * @access public
75
     * @param  string $moduleName Name of the module / entity type
76
     * @return array  Result object
77
     */
78
    public function getOne($moduleName)
79
    {
80
        return $this->wsClient->invokeOperation('describe', [ 'elementType' => $moduleName ], 'GET');
0 ignored issues
show
Bug introduced by
The method invokeOperation cannot be called on $this->wsClient (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
81
    }
82
83
    /**
84
     * Gets the entity ID prepended with module / entity type ID
85
     * @access private
86
     * @param  string       $moduleName   Name of the module / entity type
87
     * @param  string       $entityID     Numeric entity ID
88
     * @return boolean|string Returns false if it is not possible to retrieve module / entity type ID
89
     */
90
    public function getTypedID($moduleName, $entityID)
91
    {
92
        if (stripos((string)$entityID, 'x') !== false) {
93
            return $entityID;
94
        }
95
96
        if (empty($entityID) || intval($entityID) < 1) {
97
            throw new WSException('Entity ID must be a valid number');
98
        }
99
100
        $type = $this->getType($moduleName);
0 ignored issues
show
Bug introduced by
The method getType() does not exist on Salaros\Vtiger\VTWSCLib\Modules. Did you maybe mean getTypedID()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101
        if (!$type || !array_key_exists('idPrefix', $type)) {
102
            $errorMessage = sprintf("The following module is not installed: %s", $moduleName);
103
            throw new WSException($errorMessage);
104
        }
105
106
        return "{$type['idPrefix']}x{$entityID}";
107
    }
108
}
109