Test Failed
Push — master ( 19e7e7...e85ccb )
by
unknown
07:48
created

Client::userTableQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 9.9
1
<?php
2
namespace Kendox;
3
4
    require_once("class.kendox-token-generator.php");
5
6
    class Client
7
    {
8
        
9
        /**
10
         * Generated token for user
11
         * 
12
         * @var StdClass
0 ignored issues
show
Bug introduced by
The type Kendox\StdClass was not found. Did you mean StdClass? If so, make sure to prefix the type with \.
Loading history...
13
         */
14
        public $Token = null;
15
16
        /**
17
         * URL of Kendox Service Endpoint
18
         * @var string
19
         */
20
        public $ServiceEndpoint = null;
21
22
        /**
23
         * Connection Id returned from service on successful login
24
         * @var string
25
         */
26
        public $ConnectionId = null;
27
28
        function __construct($serviceEndpoint)
29
        {
30
            if (!str_ends_with($serviceEndpoint, '/')) $serviceEndpoint .= '/';
31
            $this->ServiceEndpoint = $serviceEndpoint;
32
        }
33
34
        /**
35
         * Login to kendox service by creating a user based token, signed by Kendox trusted certificate
36
         * 
37
         * @param string $pfxFile The full path and file name of the PFX-File (certificate)
38
         * @param string $pfxPassword The password used for reading the PFX-File (certificate)
39
         * @param string $userName Username for token generation
40
         * 
41
         * @return bool
42
         */
43
        public function loginWithToken($pfxFile, $pfxPassword, $userName) {
44
            try {
45
                $issuer = gethostname();
46
                $tokenGenerator = new \Kendox\TokenGenerator($issuer, $pfxFile, $pfxPassword);
47
                $this->Token = $tokenGenerator->generateToken($userName);                
0 ignored issues
show
Documentation Bug introduced by
It seems like $tokenGenerator->generateToken($userName) of type string is incompatible with the declared type Kendox\StdClass of property $Token.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
                $logonParameters = [
49
                    "tenantName" => "",
50
                    "token" => $this->Token,
51
                    "tokenType" => "InfoShareToken"
52
                ];
53
                $result = $this->post("Authentication/LogonWithToken", $logonParameters);
54
                $this->ConnectionId = $result->LogonWithTokenResult->ConnectionId;
55
                return true;
56
            } catch(\Exception $ex) {
57
                throw new \Exception("Token-Login failed: ".$ex->getMessage());
58
            }
59
        }
60
61
        /**
62
         * Logout from kendox
63
         * 
64
         * @return bool
65
         */
66
        public function logout() {
67
            try {
68
                $logoutParameters = [
69
                    "connectionId" => $this->ConnectionId
70
                ];
71
                $result = $this->post("Authentication/Logout", $logoutParameters);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
72
                $this->ConnectionId = null;
73
                return true;
74
            } catch(\Exception $ex) {
75
                throw new \Exception("Token-Login failed: ".$ex->getMessage());
76
            }
77
        }
78
79
        /**
80
         * Performs a user table query and fetch the result records
81
         * 
82
         * @param $userTableName The name of the user table
0 ignored issues
show
Bug introduced by
The type Kendox\The 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...
83
         * @param $whereClauseElements Array with fields "ColumnName", "RelationalOperator" and "Value" for filter defintion of the query
84
         * @param $addColumnHeaders Add column headers to result?
0 ignored issues
show
Bug introduced by
The type Kendox\Add 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...
85
         * 
86
         * @return array The data result as an array
87
         */
88
        public function userTableQuery($userTableName, $whereClauseElements, $addColumnHeaders) {
89
            try {
90
                $parameters = [
91
                    "connectionId" => $this->ConnectionId,
92
                    "userTable" => $userTableName,
93
                    "whereClauseElements" => $whereClauseElements,
94
                    "addColumnHeaders" => $addColumnHeaders
95
                ];
96
                $result = $this->post("UserTable/UserTableGetRecords", $parameters);
97
                if (!isset($result->UserTableGetRecordsResult)) throw new \Exception("Unexpected result");
98
                return $result->UserTableGetRecordsResult;
99
            } catch(\Exception $ex) {
100
                throw new \Exception("User table query failed: ".$ex->getMessage());
101
            }
102
        }
103
104
        /**
105
         * Uploading a file
106
         * @param string $file Path and file name of file to upload
107
         */
108
        public function uploadFile($file) {
109
            $content = file_get_contents($file);
110
            return $this->uploadContent($content);
111
        }
112
113
        /**
114
         * Uploading a stream of data
115
         * @param Stream $stream Stream of content to upload
0 ignored issues
show
Bug introduced by
The type Kendox\Stream 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...
116
         */
117
        public function uploadStream($stream) {
118
            $content = stream_get_contents($stream);
0 ignored issues
show
Bug introduced by
$stream of type Kendox\Stream is incompatible with the type resource expected by parameter $stream of stream_get_contents(). ( Ignorable by Annotation )

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

118
            $content = stream_get_contents(/** @scrutinizer ignore-type */ $stream);
Loading history...
119
            return $this->uploadContent($content);
120
        }
121
122
        private function uploadContent($content) {
123
            $base64 = base64_encode($content);
124
            $uploadParameters = [
125
                "connectionId" => $this->ConnectionId,
126
                "fileContentbase64" => $base64
127
            ];            
128
            $result = $this->post("File/UploadFileBase64", $uploadParameters);            
129
            return $result->UploadFileBase64Result;
130
        }
131
132
        /**
133
         * Performing a post request to service
134
         * 
135
         * @param string $path the route to the API endpoint (without service endpoint url)
136
         * @param string Associated array with data to post
0 ignored issues
show
Bug introduced by
The type Kendox\Associated 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...
137
         * 
138
         * @return object Returns object with data. If service returns an error an exception will be thrown with detailed information
139
         */
140
        private function post($path, $data)
141
        {
142
            $ch = curl_init();
143
            //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
144
            curl_setopt($ch, CURLOPT_URL, $this->ServiceEndpoint.$path);
145
            curl_setopt($ch, CURLOPT_POST, 1);
146
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
147
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
148
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
149
            $jsonResult = curl_exec($ch);
150
            if (curl_errno($ch)) {
151
                throw new \Exception("Error on post request: ".curl_errno($ch));
152
            }
153
            return $this->handleJsonResult($jsonResult);
154
155
        }
156
        
157
        private function handleJsonResult($json)
158
        {
159
            if ($json === FALSE) {
160
                throw new \Exception("No valid JSON has been returned from service.");
161
            }
162
            $result = json_decode($json);
163
            if (isset($result->ErrorNumber)) {
164
                throw new \Exception("(".$result->ErrorNumber.") ".$result->Message);
165
            }
166
            return $result;
167
        }
168
169
    }
170
171
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...