Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImpressionStat
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Message\Statistic
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Message\Http;
16
17use phpOMS\Localization\ISO3166TwoEnum;
18use phpOMS\Localization\ISO639x1Enum;
19
20/**
21 * Request statistic class.
22 *
23 * @package phpOMS\Message\Statistic
24 * @license OMS License 2.0
25 * @link    https://jingga.app
26 * @since   1.0.0
27 */
28class ImpressionStat
29{
30    /**
31     * Request ip
32     *
33     * @var string
34     * @since 1.0.0
35     */
36    public string $address = '';
37
38    /**
39     * Request language
40     *
41     * @var string
42     * @since 1.0.0
43     */
44    public string $language = ISO639x1Enum::_EN;
45
46    /**
47     * Request country
48     *
49     * @var string
50     * @since 1.0.0
51     */
52    public string $country = ISO3166TwoEnum::_XXX;
53
54    /**
55     * Request time
56     *
57     * @var \DateTime
58     * @since 1.0.0
59     */
60    public \DateTime $datetime;
61
62    /**
63     * Request host/domain
64     *
65     * @var string
66     * @since 1.0.0
67     */
68    public string $host = '';
69
70    /**
71     * Request path
72     *
73     * @var string
74     * @since 1.0.0
75     */
76    public string $path = '';
77
78    /**
79     * Request full uri
80     *
81     * @var string
82     * @since 1.0.0
83     */
84    public string $uri = '';
85
86    /**
87     * Request referer link
88     *
89     * @var string
90     * @since 1.0.0
91     */
92    public string $referer = '';
93
94    /**
95     * Request browser/agent
96     *
97     * @var string
98     * @since 1.0.0
99     */
100    public string $userAgent = '';
101
102    /**
103     * Additional custom meta data to be stored
104     *
105     * @var array
106     * @since 1.0.0
107     */
108    public array $meta = [];
109
110    /**
111     * Constructor.
112     *
113     * @param HttpRequest $request Http request object
114     *
115     * @since 1.0.0
116     */
117    public function __construct(HttpRequest $request)
118    {
119        $this->language  = $request->header->l11n->language;
120        $this->country   = $request->header->l11n->country;
121        $this->uri       = \substr($request->uri->uri, 0, 255);
122        $this->host      = $request->uri->host;
123        $this->path      = \substr($request->uri->path, 0, 255);
124        $this->address   = $request->header->getRequestIp();
125        $this->datetime  = new \DateTime('@' . $request->header->getRequestTime());
126        $this->referer   = \substr($request->header->getReferer(), 0, 255);
127        $this->userAgent = $request->header->getBrowserName();
128    }
129
130    /**
131     * Turn object to array
132     *
133     * @return array
134     *
135     * @since 1.0.0
136     */
137    public function toArray() : array
138    {
139        return [
140            'address'  => $this->address,
141            'date'     => $this->datetime->format('Y-m-d'),
142            'hour'     => $this->datetime->format('H'),
143            'host'     => $this->host,
144            'path'     => $this->path,
145            'uri'      => $this->uri,
146            'agent'    => $this->userAgent,
147            'language' => $this->language,
148            'country'  => $this->country,
149            'referer'  => $this->referer,
150            'datetime' => $this->datetime->getTimestamp(),
151            'meta'     => $this->meta,
152        ];
153    }
154}