coderius /
yii2-hit-counter
| 1 | <?php |
||
| 2 | |||
| 3 | namespace coderius\hitCounter\services; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\base\Component; |
||
| 7 | use yii\di\Instance; |
||
| 8 | use coderius\hitCounter\Module; |
||
| 9 | use coderius\hitCounter\components\deviceDetect\IDeviceDetect; |
||
| 10 | use yii\web\Request; |
||
| 11 | use coderius\hitCounter\traits\RequestTrait; |
||
| 12 | use coderius\hitCounter\models\HitCounterModel; |
||
| 13 | use coderius\hitCounter\repositories\HitCounterRepository; |
||
| 14 | use yii\helpers\Inflector; |
||
| 15 | use yii\base\InvalidConfigException; |
||
| 16 | use yii\helpers\StringHelper; |
||
| 17 | use coderius\hitCounter\entities\HitCounter; |
||
| 18 | use yii\helpers\ArrayHelper; |
||
| 19 | use coderius\hitCounter\dto\HitDTO; |
||
| 20 | use yii\web\BadRequestHttpException; |
||
| 21 | |||
| 22 | |||
| 23 | class HitCounterService extends Component{ |
||
| 24 | |||
| 25 | use RequestTrait; |
||
| 26 | |||
| 27 | private $deviceDetector; |
||
| 28 | private $repoHitCounter; |
||
| 29 | |||
| 30 | 21 | public function __construct(IDeviceDetect $dd, HitCounterRepository $hcr, $config = []) |
|
| 31 | { |
||
| 32 | 21 | $this->deviceDetector = $dd; |
|
| 33 | 21 | $this->repoHitCounter = $hcr; |
|
| 34 | 21 | parent::__construct($config); |
|
| 35 | |||
| 36 | 21 | } |
|
| 37 | |||
| 38 | 3 | public function create(HitCounterModel $model, string $entityClass = HitCounter::class): HitCounter |
|
| 39 | { |
||
| 40 | 3 | $dto = (new HitCounterModelDtoAssembler())->writeDto($model);//dto |
|
| 41 | 3 | $hit = (new HitCounterDtoAssembler($entityClass))->readDto($dto); |
|
| 42 | |||
| 43 | 3 | $this->repoHitCounter->save($hit); |
|
| 44 | 3 | return $hit; |
|
| 45 | } |
||
| 46 | |||
| 47 | 15 | public function loadModel(Request $request) |
|
| 48 | { |
||
| 49 | 15 | $model = new HitCounterModel(); |
|
| 50 | 15 | $model->setAttributes($this->detectJsVisitInfo($request)); |
|
| 51 | 12 | $model->setAttributes($this->detectServerVisitInfo($request)); |
|
| 52 | 12 | return $model; |
|
| 53 | } |
||
| 54 | |||
| 55 | 15 | private function detectJsVisitInfo(Request $request) |
|
| 56 | { |
||
| 57 | 15 | $array = $request->get(); |
|
| 58 | 15 | $data = []; |
|
| 59 | |||
| 60 | 15 | if(isset($array['i'])){ |
|
| 61 | 12 | $data['counter_id'] = $array['i']; |
|
| 62 | 12 | $data['js_cookei_enabled'] = ArrayHelper::getValue($array, 'c', 0); |
|
| 63 | 12 | $data['js_java_enabled'] = ArrayHelper::getValue($array, 'j', 0); |
|
| 64 | 12 | $data['js_timezone_offset'] = ArrayHelper::getValue($array, 't'); |
|
| 65 | 12 | $data['js_timezone'] = ArrayHelper::getValue($array, 'tz'); |
|
| 66 | 12 | $data['js_connection'] = ArrayHelper::getValue($array, 'cnt'); |
|
| 67 | 12 | $data['js_current_url'] = ArrayHelper::getValue($array, 'u'); |
|
| 68 | 12 | $data['js_referer_url'] = ArrayHelper::getValue($array, 'r'); |
|
| 69 | 12 | $data['js_screen_width'] = ArrayHelper::getValue($array, 'w'); |
|
| 70 | 12 | $data['js_screen_height'] = ArrayHelper::getValue($array, 'h'); |
|
| 71 | 12 | $data['js_color_depth'] = ArrayHelper::getValue($array, 'd'); |
|
| 72 | 12 | $data['js_browser_language']= ArrayHelper::getValue($array, 'lg'); |
|
| 73 | 12 | $data['js_history_length'] = ArrayHelper::getValue($array, 'hl'); |
|
| 74 | 12 | $data['js_is_toutch_device']= ArrayHelper::getValue($array, 'td', 0); |
|
| 75 | 12 | $data['js_processor_ram'] = ArrayHelper::getValue($array, 'ram'); |
|
| 76 | |||
| 77 | 12 | return $data; |
|
| 78 | }else{ |
||
| 79 | 3 | throw new BadRequestHttpException('required get param "i" cannot be empty.'); |
|
| 80 | } |
||
| 81 | |||
| 82 | } |
||
| 83 | |||
| 84 | 12 | private function detectServerVisitInfo(Request $request) |
|
| 85 | { |
||
| 86 | // $request = Yii::$app->request; |
||
| 87 | 12 | $data = []; |
|
| 88 | |||
| 89 | //Common user data |
||
| 90 | 12 | $data['serv_ip'] = $request->getUserIP(); |
|
| 91 | 12 | $data['serv_user_agent'] = $request->getUserAgent(); |
|
| 92 | 12 | $data['serv_referer_url'] = $request->getReferrer(); |
|
| 93 | 12 | $data['serv_server_name'] = $request->getServerName(); |
|
| 94 | 12 | $data['serv_auth_user_id'] = \Yii::$app->user->isGuest ? null : Yii::$app->user->identity->id; |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 95 | |||
| 96 | 12 | $data['serv_port'] = $request->getPort(); |
|
| 97 | 12 | $data['serv_cookies'] = \yii\helpers\Json::encode($request->getCookies()); |
|
| 98 | |||
| 99 | //Device detect |
||
| 100 | 12 | $data['serv_os'] = \yii\helpers\Json::encode($this->deviceDetector->getOs()); |
|
| 101 | 12 | $data['serv_client'] = \yii\helpers\Json::encode($this->deviceDetector->getClient()); |
|
| 102 | 12 | $data['serv_device'] = $this->deviceDetector->getDeviceName(); |
|
| 103 | 12 | $data['serv_brand'] = $this->deviceDetector->getBrandName(); |
|
| 104 | 12 | $data['serv_model'] = $this->deviceDetector->getModel(); |
|
| 105 | 12 | $data['serv_bot'] = $this->deviceDetector->getBot(); |
|
| 106 | |||
| 107 | //Organizacion provider detect |
||
| 108 | 12 | $data['serv_host_by_ip'] = $this->getHostByAddr(); |
|
| 109 | 12 | $data['serv_is_proxy_or_vpn'] = (int)$this->isProxyVisit(); |
|
| 110 | |||
| 111 | //Set mark cookie |
||
| 112 | 12 | $data['cookie_mark'] = static::pastCookieMark(); |
|
| 113 | |||
| 114 | 12 | return $data; |
|
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | 12 | private static function pastCookieMark() |
|
| 119 | { |
||
| 120 | 12 | $request = Yii::$app->request; |
|
|
0 ignored issues
–
show
It seems like
Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. 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 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...
|
|||
| 121 | 12 | $response = Yii::$app->response; |
|
|
0 ignored issues
–
show
It seems like
Yii::app->response can also be of type yii\web\Response. However, the property $response is declared as type yii\console\Response. 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 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...
|
|||
| 122 | |||
| 123 | 12 | $name = static::defaultNameCookieMark(); |
|
| 124 | |||
| 125 | 12 | if ($request->cookies->has($name)){ |
|
| 126 | return $request->cookies->getValue($name); |
||
| 127 | }else{ |
||
| 128 | 12 | $str = Yii::$app->getSecurity()->generateRandomString(); |
|
| 129 | |||
| 130 | // add to HTTP |
||
| 131 | 12 | $response->cookies->add(new \yii\web\Cookie([ |
|
| 132 | 12 | 'name' => $name, |
|
| 133 | 12 | 'value' => $str, |
|
| 134 | ])); |
||
| 135 | |||
| 136 | 12 | return $str; |
|
| 137 | } |
||
| 138 | // throw new InvalidConfigException('Invalid actual name "' . gettype($actualName) . '" specified at "' . get_class($this) . '::normalizeUserAttributeMap"'); |
||
| 139 | } |
||
| 140 | |||
| 141 | 15 | public static function defaultNameCookieMark() |
|
| 142 | { |
||
| 143 | 15 | return Inflector::camel2id(StringHelper::basename(__CLASS__)); |
|
| 144 | } |
||
| 145 | |||
| 146 | } |