Skip to content

Commit bc95e7e

Browse files
committed
Update coding style
1 parent 85a4268 commit bc95e7e

File tree

10 files changed

+262
-256
lines changed

10 files changed

+262
-256
lines changed

src/Config/Events.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?php namespace Tatter\Visits\Config;
1+
<?php
2+
3+
namespace Tatter\Visits\Config;
24

35
use CodeIgniter\Events\Events;
4-
use Config\Services;
56

6-
Events::on('post_controller_constructor', fn() => // Ignore CLI requests
7+
Events::on('post_controller_constructor', static fn () => // Ignore CLI requests
78
is_cli() ?: service('visits')->record());

src/Config/Services.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
<?php namespace Tatter\Visits\Config;
1+
<?php
2+
3+
namespace Tatter\Visits\Config;
24

35
use Config\Services as BaseServices;
46
use Tatter\Visits\Config\Visits as VisitsConfig;
57
use Tatter\Visits\Visits;
68

79
class Services extends BaseServices
810
{
9-
public static function visits(VisitsConfig $config = null, bool $getShared = true)
10-
{
11-
if ($getShared)
12-
{
13-
return static::getSharedInstance('visits', $config);
14-
}
11+
public static function visits(?VisitsConfig $config = null, bool $getShared = true)
12+
{
13+
if ($getShared) {
14+
return static::getSharedInstance('visits', $config);
15+
}
1516

16-
$config ??= config('Visits');
17+
$config ??= config('Visits');
1718

18-
return new Visits($config);
19-
}
19+
return new Visits($config);
20+
}
2021
}

src/Config/Visits.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
<?php namespace Tatter\Visits\Config;
1+
<?php
2+
3+
namespace Tatter\Visits\Config;
24

35
use CodeIgniter\Config\BaseConfig;
46

57
class Visits extends BaseConfig
68
{
7-
// metric for tracking a unique visitor
8-
// one of: ip_address, session_id, user_id
9-
public $trackingMethod = 'ip_address';
9+
// metric for tracking a unique visitor
10+
// one of: ip_address, session_id, user_id
11+
public $trackingMethod = 'ip_address';
1012

11-
// the session variable to check for a logged-in user ID
12-
public $userSource = 'logged_in';
13+
// the session variable to check for a logged-in user ID
14+
public $userSource = 'logged_in';
1315

14-
// how many minutes before a visit counts as new instead of incrementing a previous view count
15-
// set to zero to record each page view as unique (not recommended)
16-
public $resetMinutes = 60;
16+
// how many minutes before a visit counts as new instead of incrementing a previous view count
17+
// set to zero to record each page view as unique (not recommended)
18+
public $resetMinutes = 60;
1719

18-
// Whether to ignore AJAX requests when recording
19-
public $ignoreAjax = true;
20+
// Whether to ignore AJAX requests when recording
21+
public $ignoreAjax = true;
2022
}
Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
<?php namespace Tatter\Visits\Database\Migrations;
1+
<?php
2+
3+
namespace Tatter\Visits\Database\Migrations;
24

35
use CodeIgniter\Database\Migration;
46

57
class Migration_create_table_visits extends Migration
68
{
7-
public function up()
8-
{
9-
$fields = [
10-
'session_id' => ['type' => 'varchar', 'constraint' => 32, 'default' => ''],
11-
'user_id' => ['type' => 'int', 'null' => true],
12-
'ip_address' => ['type' => 'bigint', 'null' => true],
13-
'user_agent' => ['type' => 'varchar', 'constraint' => 255, 'default' => ''],
14-
'scheme' => ['type' => 'varchar', 'constraint' => 15, 'default' => ''],
15-
'host' => ['type' => 'varchar', 'constraint' => 63],
16-
'port' => ['type' => 'varchar', 'constraint' => 15, 'default' => ''],
17-
'user' => ['type' => 'varchar', 'constraint' => 31, 'default' => ''],
18-
'pass' => ['type' => 'varchar', 'constraint' => 255, 'default' => ''],
19-
'path' => ['type' => 'varchar', 'constraint' => 255],
20-
'query' => ['type' => 'varchar', 'constraint' => 255, 'default' => ''],
21-
'fragment' => ['type' => 'varchar', 'constraint' => 31, 'default' => ''],
22-
'views' => ['type' => 'int', 'default' => 1],
23-
'created_at' => ['type' => 'datetime', 'null' => true],
24-
'updated_at' => ['type' => 'datetime', 'null' => true],
25-
];
26-
27-
$this->forge->addField('id');
28-
$this->forge->addField($fields);
9+
public function up()
10+
{
11+
$fields = [
12+
'session_id' => ['type' => 'varchar', 'constraint' => 32, 'default' => ''],
13+
'user_id' => ['type' => 'int', 'null' => true],
14+
'ip_address' => ['type' => 'bigint', 'null' => true],
15+
'user_agent' => ['type' => 'varchar', 'constraint' => 255, 'default' => ''],
16+
'scheme' => ['type' => 'varchar', 'constraint' => 15, 'default' => ''],
17+
'host' => ['type' => 'varchar', 'constraint' => 63],
18+
'port' => ['type' => 'varchar', 'constraint' => 15, 'default' => ''],
19+
'user' => ['type' => 'varchar', 'constraint' => 31, 'default' => ''],
20+
'pass' => ['type' => 'varchar', 'constraint' => 255, 'default' => ''],
21+
'path' => ['type' => 'varchar', 'constraint' => 255],
22+
'query' => ['type' => 'varchar', 'constraint' => 255, 'default' => ''],
23+
'fragment' => ['type' => 'varchar', 'constraint' => 31, 'default' => ''],
24+
'views' => ['type' => 'int', 'default' => 1],
25+
'created_at' => ['type' => 'datetime', 'null' => true],
26+
'updated_at' => ['type' => 'datetime', 'null' => true],
27+
];
28+
29+
$this->forge->addField('id');
30+
$this->forge->addField($fields);
31+
32+
$this->forge->addKey('session_id');
33+
$this->forge->addKey('user_id');
34+
$this->forge->addKey('ip_address');
35+
$this->forge->addKey(['host', 'path']);
36+
$this->forge->addKey('created_at');
37+
$this->forge->addKey('updated_at');
2938

30-
$this->forge->addKey('session_id');
31-
$this->forge->addKey('user_id');
32-
$this->forge->addKey('ip_address');
33-
$this->forge->addKey(['host', 'path']);
34-
$this->forge->addKey('created_at');
35-
$this->forge->addKey('updated_at');
36-
37-
$this->forge->createTable('visits');
38-
}
39+
$this->forge->createTable('visits');
40+
}
3941

40-
public function down()
41-
{
42-
$this->forge->dropTable('visits');
43-
}
42+
public function down()
43+
{
44+
$this->forge->dropTable('visits');
45+
}
4446
}

src/Entities/Visit.php

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,55 @@
1-
<?php namespace Tatter\Visits\Entities;
1+
<?php
2+
3+
namespace Tatter\Visits\Entities;
24

35
use CodeIgniter\Entity\Entity;
46
use Tatter\Visits\Models\VisitModel;
57

68
class Visit extends Entity
79
{
8-
protected $dates = [
9-
'created_at',
10-
'verified_at',
11-
];
12-
13-
// magic IP string/long converters
14-
public function setIpAddress($ipAddress)
15-
{
16-
$this->attributes['ip_address'] = ($long = ip2long($ipAddress)) ? $long : $ipAddress;
17-
18-
return $this;
19-
}
20-
21-
public function getIpAddress(string $format = 'long')
22-
{
23-
if ($format === 'string')
24-
{
25-
return long2ip($this->attributes['ip_address']);
26-
}
10+
protected $dates = [
11+
'created_at',
12+
'verified_at',
13+
];
14+
15+
// magic IP string/long converters
16+
public function setIpAddress($ipAddress)
17+
{
18+
$this->attributes['ip_address'] = ($long = ip2long($ipAddress)) ? $long : $ipAddress;
19+
20+
return $this;
21+
}
22+
23+
public function getIpAddress(string $format = 'long')
24+
{
25+
if ($format === 'string') {
26+
return long2ip($this->attributes['ip_address']);
27+
}
2728

2829
return $this->attributes['ip_address'];
29-
}
30-
31-
// search for a visit with similar characteristics to the current one
32-
public function getSimilar($trackingMethod, $resetMinutes = 60)
33-
{
34-
// required fields
35-
if (empty($this->host) || empty($this->path))
36-
{
37-
return false;
38-
}
39-
// require tracking field
40-
if (empty($this->{$trackingMethod}))
41-
{
42-
return false;
43-
}
44-
45-
$visits = new VisitModel();
46-
// check for matching components within the last resetMinutes
47-
$since = date('Y-m-d H:i:s', strtotime('-' . $resetMinutes . ' minutes'));
48-
49-
return $visits->where('host', $this->host)
50-
->where('path', $this->path)
51-
->where('query', (string)$this->query)
52-
->where($trackingMethod, $this->{$trackingMethod})
53-
->where('created_at >=', $since)
54-
->first();
55-
}
30+
}
31+
32+
// search for a visit with similar characteristics to the current one
33+
public function getSimilar($trackingMethod, $resetMinutes = 60)
34+
{
35+
// required fields
36+
if (empty($this->host) || empty($this->path)) {
37+
return false;
38+
}
39+
// require tracking field
40+
if (empty($this->{$trackingMethod})) {
41+
return false;
42+
}
43+
44+
$visits = new VisitModel();
45+
// check for matching components within the last resetMinutes
46+
$since = date('Y-m-d H:i:s', strtotime('-' . $resetMinutes . ' minutes'));
47+
48+
return $visits->where('host', $this->host)
49+
->where('path', $this->path)
50+
->where('query', (string) $this->query)
51+
->where($trackingMethod, $this->{$trackingMethod})
52+
->where('created_at >=', $since)
53+
->first();
54+
}
5655
}

src/Exceptions/VisitsException.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
<?php namespace Tatter\Visits\Exceptions;
1+
<?php
2+
3+
namespace Tatter\Visits\Exceptions;
24

35
use CodeIgniter\Exceptions\ExceptionInterface;
46
use CodeIgniter\Exceptions\FrameworkException;
57

68
class VisitsException extends FrameworkException implements ExceptionInterface
79
{
8-
public static function forNoTrackingMethod()
9-
{
10-
return new static(lang('Visits.noTrackingMethod'));
11-
}
10+
public static function forNoTrackingMethod()
11+
{
12+
return new static(lang('Visits.noTrackingMethod'));
13+
}
1214

13-
public static function forInvalidResetMinutes()
14-
{
15-
return new static(lang('Visits.invalidResetMinutes'));
16-
}
15+
public static function forInvalidResetMinutes()
16+
{
17+
return new static(lang('Visits.invalidResetMinutes'));
18+
}
1719

18-
public static function forMissingDatabaseTable(string $table)
19-
{
20-
return new static(lang('Visits.missingDatabaseTable', [$table]));
21-
}
20+
public static function forMissingDatabaseTable(string $table)
21+
{
22+
return new static(lang('Visits.missingDatabaseTable', [$table]));
23+
}
2224
}

src/Language/en/Visits.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Tatter\Visits\Language\en;
44

55
return [
6-
'noTrackingMethod' => 'No tracking method selected.',
7-
'invalidResetMinutes' => 'Minutes-to-reset must be a positive integer or zero.',
8-
'missingDatabaseTable' => 'Table "{0}" missing for visit storage.',
6+
'noTrackingMethod' => 'No tracking method selected.',
7+
'invalidResetMinutes' => 'Minutes-to-reset must be a positive integer or zero.',
8+
'missingDatabaseTable' => 'Table "{0}" missing for visit storage.',
99
];

src/Models/VisitModel.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
<?php namespace Tatter\Visits\Models;
1+
<?php
2+
3+
namespace Tatter\Visits\Models;
24

35
use CodeIgniter\Model;
46

57
class VisitModel extends Model
68
{
7-
protected $table = 'visits';
8-
protected $primaryKey = 'id';
9-
protected $returnType = 'Tatter\Visits\Entities\Visit';
10-
11-
protected $useTimestamps = true;
12-
protected $useSoftDeletes = false;
13-
protected $skipValidation = false;
14-
15-
protected $allowedFields = [
16-
'session_id',
17-
'user_id',
18-
'ip_address',
19-
'user_agent',
20-
'views',
21-
'scheme',
22-
'host',
23-
'port',
24-
'user',
25-
'pass',
26-
'path',
27-
'query',
28-
'fragment',
29-
];
30-
31-
protected $validationRules = [
32-
'host' => 'required',
33-
'path' => 'required',
34-
];
9+
protected $table = 'visits';
10+
protected $primaryKey = 'id';
11+
protected $returnType = 'Tatter\Visits\Entities\Visit';
12+
protected $useTimestamps = true;
13+
protected $useSoftDeletes = false;
14+
protected $skipValidation = false;
15+
protected $allowedFields = [
16+
'session_id',
17+
'user_id',
18+
'ip_address',
19+
'user_agent',
20+
'views',
21+
'scheme',
22+
'host',
23+
'port',
24+
'user',
25+
'pass',
26+
'path',
27+
'query',
28+
'fragment',
29+
];
30+
protected $validationRules = [
31+
'host' => 'required',
32+
'path' => 'required',
33+
];
3534
}

0 commit comments

Comments
 (0)