Completed
Push — master ( 52586c...0ccbd9 )
by Florian
11:29
created

RequestService::isAuthenticationCodeValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: famoser
5
 * Date: 14.11.2016
6
 * Time: 12:39
7
 */
8
9
namespace Famoser\SyncApi\Services;
10
11
12
use Famoser\SyncApi\Framework\Json\Models\ObjectProperty;
13
use Famoser\SyncApi\Framework\Json\SimpleJsonMapper;
14
use Famoser\SyncApi\Interfaces\JsonDeserializableInterface;
15
use Famoser\SyncApi\Models\Communication\Request\AuthorizationRequest;
16
use Famoser\SyncApi\Models\Communication\Request\CollectionEntityRequest;
17
use Famoser\SyncApi\Models\Communication\Request\HistoryEntityRequest;
18
use Famoser\SyncApi\Models\Communication\Request\SyncEntityRequest;
19
use Famoser\SyncApi\Services\Base\BaseService;
20
use Famoser\SyncApi\Services\Interfaces\RequestServiceInterface;
21
use Slim\Http\Request;
22
23
/**
24
 * the request service parses & validates requests
25
 *
26
 * @package Famoser\SyncApi\Services
27
 */
28
class RequestService extends BaseService implements RequestServiceInterface
29
{
30
    /**
31
     * @param Request $request
32
     * @return AuthorizationRequest
33
     * @throws \JsonMapper_Exception
34
     */
35
    public function parseAuthorizationRequest(Request $request)
36
    {
37
        return $this->executeJsonMapper($request, new AuthorizationRequest());
38
    }
39
40
    /**
41
     * @param Request $request
42
     * @return CollectionEntityRequest
43
     * @throws \JsonMapper_Exception
44
     */
45
    public function parseCollectionEntityRequest(Request $request)
46
    {
47
        return $this->executeJsonMapper($request, new CollectionEntityRequest());
48
    }
49
50
    /**
51
     * @param Request $request
52
     * @return HistoryEntityRequest
53
     * @throws \JsonMapper_Exception
54
     */
55
    public function parseHistoryEntityRequest(Request $request)
56
    {
57
        return $this->executeJsonMapper($request, new HistoryEntityRequest());
58
    }
59
60
    /**
61
     * @param Request $request
62
     * @return SyncEntityRequest
63
     * @throws \JsonMapper_Exception
64
     */
65
    public function parseSyncEntityRequest(Request $request)
66
    {
67
        return $this->executeJsonMapper($request, new SyncEntityRequest());
68
    }
69
70
    /**
71
     * @param Request $request
72
     * @param $model
73
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
74
     * @throws \JsonMapper_Exception
75
     */
76
    private function executeJsonMapper(Request $request, JsonDeserializableInterface $model)
77
    {
78
        $json = $request->getBody()->getContents();
79
        $mapper = new SimpleJsonMapper();
80
        $objProp = new ObjectProperty('root', $model);
81
        $resObj = $mapper->mapObject($json, $objProp);
82
        $this->getLoggingService()->log(json_encode($resObj, JSON_PRETTY_PRINT), 'RequestHelper.txt');
83
        return $resObj;
84
    }
85
86
    /**
87
     * @param string $authCode
88
     * @param int $applicationSeed
89
     * @param int $personSeed
90
     * @param int $apiModulo
91
     * @param int $requestCount
92
     * @param int $requestMagicNumber
93
     * @return bool
94
     */
95
    public function isAuthenticationCodeValid($authCode, $applicationSeed, $personSeed, $apiModulo, $requestCount, $requestMagicNumber)
96
    {
97
        /* C#:
98
            var authCode = apiRoamingEntity.PersonalSeed * apiRoamingEntity.RequestCount + requestMagicNumber * info.ApplicationSeed;
99
            return (authCode % info.ApiModulo).ToString();
100
101
           php:
102
            $expectedAuthCode = $personSeed * $requestCount + $requestMagicNumber * $applicationSeed;
103
            return ($expectedAuthCode % $apiModulo) == $authCode;
104
        */
105
        //to come in future version
106
        return true;
107
    }
108
}