diff --git a/lib/BackgroundJob/TrainJobIpV4.php b/lib/BackgroundJob/TrainJobIpV4.php index f61c4188..99bab3fc 100644 --- a/lib/BackgroundJob/TrainJobIpV4.php +++ b/lib/BackgroundJob/TrainJobIpV4.php @@ -15,20 +15,16 @@ use OCA\SuspiciousLogin\Service\TrainService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Throwable; class TrainJobIpV4 extends TimedJob { - /** @var TrainService */ - private $trainService; - - /** @var ILogger */ - private $logger; - - public function __construct(TrainService $trainService, - ILogger $logger, - ITimeFactory $time) { + public function __construct( + private TrainService $trainService, + private LoggerInterface $logger, + ITimeFactory $time, + ) { parent::__construct($time); $this->setInterval(24 * 60 * 60); @@ -38,8 +34,6 @@ public function __construct(TrainService $trainService, if (defined('\OCP\BackgroundJob\IJob::TIME_INSENSITIVE') && method_exists($this, 'setTimeSensitivity')) { $this->setTimeSensitivity(self::TIME_INSENSITIVE); } - $this->trainService = $trainService; - $this->logger = $logger; } /** @@ -56,15 +50,9 @@ protected function run($argument) { $strategy ); } catch (InsufficientDataException $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::INFO, - 'message' => 'No suspicious login model for IPv4 trained because of insufficient data', - ]); + $this->logger->info('No suspicious login model for IPv4 trained because of insufficient data', ['exception' => $ex]); } catch (Throwable $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::ERROR, - 'message' => 'Caught unknown error during IPv4 background training', - ]); + $this->logger->error('Caught unknown error during IPv4 background training', ['exception' => $ex]); } } } diff --git a/lib/BackgroundJob/TrainJobIpV6.php b/lib/BackgroundJob/TrainJobIpV6.php index bbb348de..bf236d0d 100644 --- a/lib/BackgroundJob/TrainJobIpV6.php +++ b/lib/BackgroundJob/TrainJobIpV6.php @@ -15,20 +15,16 @@ use OCA\SuspiciousLogin\Service\TrainService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Throwable; class TrainJobIpV6 extends TimedJob { - /** @var TrainService */ - private $trainService; - - /** @var ILogger */ - private $logger; - - public function __construct(TrainService $trainService, - ILogger $logger, - ITimeFactory $time) { + public function __construct( + private TrainService $trainService, + private LoggerInterface $logger, + ITimeFactory $time, + ) { parent::__construct($time); $this->setInterval(24 * 60 * 60); @@ -38,8 +34,6 @@ public function __construct(TrainService $trainService, if (defined('\OCP\BackgroundJob\IJob::TIME_INSENSITIVE') && method_exists($this, 'setTimeSensitivity')) { $this->setTimeSensitivity(self::TIME_INSENSITIVE); } - $this->trainService = $trainService; - $this->logger = $logger; } /** @@ -56,15 +50,9 @@ protected function run($argument) { $strategy ); } catch (InsufficientDataException $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::INFO, - 'message' => 'No suspicious login model for IPv6 trained because of insufficient data', - ]); + $this->logger->info('No suspicious login model for IPv6 trained because of insufficient data', ['exception' => $ex]); } catch (Throwable $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::ERROR, - 'message' => 'Caught unknown error during IPv6 background training', - ]); + $this->logger->error('Caught unknown error during IPv6 background training', ['exception' => $ex]); } } } diff --git a/lib/Listener/LoginMailListener.php b/lib/Listener/LoginMailListener.php index 659b0cff..5ad72a07 100644 --- a/lib/Listener/LoginMailListener.php +++ b/lib/Listener/LoginMailListener.php @@ -14,38 +14,24 @@ use OCP\EventDispatcher\IEventListener; use OCP\IConfig; use OCP\IL10N; -use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; use OCP\Mail\IMailer; use OCP\Mail\IMessage; +use Psr\Log\LoggerInterface; /** * @implements IEventListener */ class LoginMailListener implements IEventListener { - /** @var ILogger */ - private $logger; - - /** @var IMailer */ - private $mailer; - - /** @var IUserManager */ - private $userManager; - - /** @var IL10N */ - private $l; - - /** @var IConfig */ - protected $config; - - public function __construct(ILogger $logger, IMailer $mailer, IUserManager $userManager, IL10N $l, IConfig $config) { - $this->logger = $logger; - $this->mailer = $mailer; - $this->userManager = $userManager; - $this->l = $l; - $this->config = $config; + public function __construct( + private LoggerInterface $logger, + private IMailer $mailer, + private IUserManager $userManager, + private IL10N $l, + private IConfig $config, + ) { } public function handle(Event $event): void { @@ -69,10 +55,7 @@ public function handle(Event $event): void { $this->getMail($event, $user) ); } catch (Exception $e) { - $this->logger->logException($e, [ - 'message' => "Could not send suspicious login email to <$uid>", - 'level' => ILogger::ERROR, - ]); + $this->logger->error("Could not send suspicious login email to <$uid>", ['exception' => $e]); } } diff --git a/lib/Listener/LoginNotificationListener.php b/lib/Listener/LoginNotificationListener.php index 639f322c..74ff847d 100644 --- a/lib/Listener/LoginNotificationListener.php +++ b/lib/Listener/LoginNotificationListener.php @@ -13,27 +13,19 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\ILogger; use OCP\Notification\IManager; +use Psr\Log\LoggerInterface; /** * @implements IEventListener */ class LoginNotificationListener implements IEventListener { - /** @var IManager */ - private $notificationManager; - /** @var ITimeFactory */ - private $timeFactory; - /** @var ILogger */ - private $logger; - - public function __construct(IManager $notificationManager, - ITimeFactory $timeFactory, - ILogger $logger) { - $this->notificationManager = $notificationManager; - $this->timeFactory = $timeFactory; - $this->logger = $logger; + public function __construct( + private IManager $notificationManager, + private ITimeFactory $timeFactory, + private LoggerInterface $logger, + ) { } public function handle(Event $event): void { @@ -52,8 +44,7 @@ public function handle(Event $event): void { ]); $this->notificationManager->notify($notification); } catch (\Throwable $ex) { - $this->logger->critical("could not send notification about a suspicious login"); - $this->logger->logException($ex); + $this->logger->critical('Could not send notification about a suspicious login', ['exception' => $ex]); } } } diff --git a/lib/Service/ETLService.php b/lib/Service/ETLService.php index 8f388407..c99cc181 100644 --- a/lib/Service/ETLService.php +++ b/lib/Service/ETLService.php @@ -13,28 +13,18 @@ use OCA\SuspiciousLogin\Db\LoginAddressAggregatedMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Output\OutputInterface; class ETLService { public const MAX_BATCH_SIZE = 10000; - /** @var IDBConnection */ - private $db; - - /** @var LoginAddressAggregatedMapper */ - private $aggregatedMapper; - - /** @var ILogger */ - private $logger; - - public function __construct(IDBConnection $db, - LoginAddressAggregatedMapper $aggregatedMapper, - ILogger $logger) { - $this->db = $db; - $this->aggregatedMapper = $aggregatedMapper; - $this->logger = $logger; + public function __construct( + private IDBConnection $db, + private LoginAddressAggregatedMapper $aggregatedMapper, + private LoggerInterface $logger, + ) { } private function getRaw(int $max, ?OutputInterface $output = null): Generator { diff --git a/lib/Service/EstimatorService.php b/lib/Service/EstimatorService.php index 0becd13e..ada3f458 100644 --- a/lib/Service/EstimatorService.php +++ b/lib/Service/EstimatorService.php @@ -10,22 +10,16 @@ namespace OCA\SuspiciousLogin\Service; use OCA\SuspiciousLogin\Exception\ServiceException; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Rubix\ML\Datasets\Unlabeled; use RuntimeException; class EstimatorService { - /** @var ModelStore */ - private $modelStore; - - /** @var ILogger */ - private $logger; - - public function __construct(ModelStore $modelStore, - ILogger $logger) { - $this->modelStore = $modelStore; - $this->logger = $logger; + public function __construct( + private ModelStore $modelStore, + private LoggerInterface $logger, + ) { } /** diff --git a/lib/Service/LoginClassifier.php b/lib/Service/LoginClassifier.php index 44dd4fb9..47d3a743 100644 --- a/lib/Service/LoginClassifier.php +++ b/lib/Service/LoginClassifier.php @@ -16,8 +16,8 @@ use OCA\SuspiciousLogin\Util\AddressClassifier; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; -use OCP\ILogger; use OCP\IRequest; +use Psr\Log\LoggerInterface; use Throwable; use function base64_decode; use function explode; @@ -27,36 +27,14 @@ class LoginClassifier { - /** @var EstimatorService */ - private $estimator; - - /** @var IRequest */ - private $request; - - /** @var ILogger */ - private $logger; - - /** @var SuspiciousLoginMapper */ - private $mapper; - - /** @var ITimeFactory */ - private $timeFactory; - - /** @var IEventDispatcher */ - private $dispatcher; - - public function __construct(EstimatorService $estimator, - IRequest $request, - ILogger $logger, - SuspiciousLoginMapper $mapper, - ITimeFactory $timeFactory, - IEventDispatcher $dispatcher) { - $this->estimator = $estimator; - $this->request = $request; - $this->logger = $logger; - $this->mapper = $mapper; - $this->timeFactory = $timeFactory; - $this->dispatcher = $dispatcher; + public function __construct( + private EstimatorService $estimator, + private IRequest $request, + private LoggerInterface $logger, + private SuspiciousLoginMapper $mapper, + private ITimeFactory $timeFactory, + private IEventDispatcher $dispatcher, + ) { } /** @@ -126,8 +104,7 @@ private function persistSuspiciousLogin(string $uid, string $ip): ?SuspiciousLog return $entity; } catch (Throwable $ex) { - $this->logger->critical("could not save the details of a suspicious login"); - $this->logger->logException($ex); + $this->logger->critical('could not save the details of a suspicious login', ['exception' => $ex]); return null; } } diff --git a/lib/Service/ModelStore.php b/lib/Service/ModelStore.php index 3f1c1235..f237e90f 100644 --- a/lib/Service/ModelStore.php +++ b/lib/Service/ModelStore.php @@ -18,8 +18,8 @@ use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\ICacheFactory; -use OCP\ILogger; use OCP\ITempManager; +use Psr\Log\LoggerInterface; use Rubix\ML\Estimator; use Rubix\ML\Persistable; use Rubix\ML\Persisters\Filesystem; @@ -32,36 +32,14 @@ class ModelStore { public const APPDATA_MODELS_FOLDER = 'models'; - /** @var ModelMapper */ - private $modelMapper; - - /** @var IAppData */ - private $appData; - - /** @var IAppManager */ - private $appManager; - - /** @var ITempManager */ - private $tempManager; - - /** @var ICacheFactory */ - private $cacheFactory; - - /** @var ILogger */ - private $logger; - - public function __construct(ModelMapper $modelMapper, - IAppData $appData, - IAppManager $appManager, - ITempManager $tempManager, - ICacheFactory $cachFactory, - ILogger $logger) { - $this->appData = $appData; - $this->appManager = $appManager; - $this->modelMapper = $modelMapper; - $this->tempManager = $tempManager; - $this->cacheFactory = $cachFactory; - $this->logger = $logger; + public function __construct( + private ModelMapper $modelMapper, + private IAppData $appData, + private IAppManager $appManager, + private ITempManager $tempManager, + private ICacheFactory $cacheFactory, + private LoggerInterface $logger, + ) { } /**