Skip to content

Commit

Permalink
Add initial set of files
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper committed Apr 14, 2020
0 parents commit b022b11
Show file tree
Hide file tree
Showing 70 changed files with 5,723 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.idea/
.php_cs.cache
.phpunit.result.cache
/vendor/
/tests/coverage/
composer.lock
71 changes: 71 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

$config = PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'@PHP73Migration' => true,
'@PHP71Migration:risky' => true,
'@DoctrineAnnotation' => true,
'@PHPUnit75Migration:risky' => true,
'blank_line_before_statement' => [
'statements' => [
'break',
// 'case', -> On ne souhaite pas ce cas
'continue',
'declare',
// 'default', -> On ne souhaite pas ce cas
'exit',
'goto',
'include',
'include_once',
'require',
'require_once',
'return',
'switch',
'throw',
'try',
],
],
'date_time_immutable' => false,
'declare_strict_types' => false,
'general_phpdoc_annotation_remove' => [
'annotations' => [
'expectedException',
'expectedExceptionMessage',
'expectedExceptionMessageRegExp',
],
],
'global_namespace_import' => true,
'list_syntax' => ['syntax' => 'short'],
'mb_str_functions' => true,
'nullable_type_declaration_for_default_null_value' => true,
'ordered_interfaces' => true,
'phpdoc_line_span' => true,
// 'phpdoc_to_param_type' => true,
// 'phpdoc_to_return_type' => true,
// 'regular_callable_call' => true,
'self_static_accessor' => true,
// 'simplified_if_return' => true, // Fait bugger le cs-fixer (local principalement) en version < 3
'simplified_null_return' => true,
'php_unit_test_class_requires_covers' => false,
])
->setFinder(PhpCsFixer\Finder::create()->in(__DIR__))
;

// special handling of fabbot.io service if it's using too old PHP CS Fixer version
try {
PhpCsFixer\FixerFactory::create()
->registerBuiltInFixers()
->registerCustomFixers($config->getCustomFixers())
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()));
} catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
$config->setRules([]);
} catch (UnexpectedValueException $e) {
$config->setRules([]);
} catch (InvalidArgumentException $e) {
$config->setRules([]);
}

return $config;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright 2020 Damien Harper

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Auditor library
59 changes: 59 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "damienharper/auditor",
"type": "library",
"description": "Audits logs made easy.",
"keywords": ["doctrine", "audit", "audit-log", "audit-trail"],
"license": "MIT",
"authors": [
{
"name": "Damien Harper",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.2",
"doctrine/annotations": "^1.8",
"doctrine/doctrine-bundle": "^1.9|^2.0",
"doctrine/orm": "^2.7",
"symfony/event-dispatcher": "^3.4|^4.0|^5.0",
"symfony/options-resolver": "^3.4|^4.0|^5.0"
},
"autoload": {
"psr-4": {
"DH\\Auditor\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"DH\\Auditor\\Tests\\": "tests"
}
},
"require-dev": {
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0",
"friendsofphp/php-cs-fixer": "^2.16",
"gedmo/doctrine-extensions": "^2.4",
"korbeil/phpstan-generic-rules": "^0.2",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-doctrine": "^0.12",
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/phpunit": "^7.0|^8.0",
"symfony/var-dumper": "^4.0|^5.0"
},
"conflict": {
"doctrine/persistence": "<1.3"
},
"scripts": {
"test": "php -d pcov.enabled=1 ./vendor/bin/phpunit",
"csfixer": "vendor/bin/php-cs-fixer fix --config=.php_cs --using-cache=no --verbose",
"phpstan": "vendor/bin/phpstan analyse src"
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
}
}
28 changes: 28 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
includes:
- vendor/phpstan/phpstan-doctrine/extension.neon
- vendor/phpstan/phpstan-doctrine/rules.neon

parameters:
level: max
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false

# excludes_analyse:
# - '%currentWorkingDirectory%/src/DoctrineAuditBundle/DependencyInjection/Configuration.php'
#
# ignoreErrors:
# # false positives
# - '~Parameter \#1 \$name of method Symfony\\Component\\Console\\Command\\Command\:\:setName\(\) expects string, string\|null given~'
# - '~Parameter \#1 \$tableName of method Doctrine\\DBAL\\Schema\\Schema\:\:(has|get)Table\(\) expects string, string\|null given~'
# - '~Parameter \#1 \$(first|max)Results? of method Doctrine\\DBAL\\Query\\QueryBuilder\:\:set(First|Max)Results?\(\) expects int, null given~'
# - '~Parameter \#1 \$event of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects object, string given~'
# - '~Parameter \#2 \$eventName of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects string\|null, DH\\DoctrineAuditBundle\\Event\\LifecycleEvent given\.~'
# - '~Cannot call method fetchColumn\(\) on Doctrine\\DBAL\\Driver\\Statement\|int~'
# - '~Cannot call method fetchAll\(\) on Doctrine\\DBAL\\Driver\\Statement\|int~'
# - '~Cannot cast array\<string\>\|string\|null to string\.~'
# - '~Cannot cast array\<string\>\|string\|null to int\.~'
# - '~Call to an undefined method Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface\:\:getRoles\(\)\.~'
# - '~Call to method getSource\(\) on an unknown class Symfony\\Component\\Security\\Core\\Role\\SwitchUserRole\.~'
# - '~Class Symfony\\Component\\Security\\Core\\Role\\SwitchUserRole not found\.~'
# - '~Method DH\\DoctrineAuditBundle\\User\\UserInterface\:\:getId\(\) has no return typehint specified\.~'
35 changes: 35 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">

<logging>
<log type="coverage-html" target="tests/coverage" />
</logging>

<testsuites>
<testsuite name="Auditor test suite">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<!-- <exclude>-->
<!-- <file>src/DoctrineAuditBundle/DHDoctrineAuditBundle.php</file>-->
<!-- <file>src/DoctrineAuditBundle/Exception/AccessDeniedException.php</file>-->
<!-- <file>src/DoctrineAuditBundle/Exception/InvalidArgumentException.php</file>-->
<!-- <file>src/DoctrineAuditBundle/Exception/UpdateException.php</file>-->
<!-- <file>src/DoctrineAuditBundle/User/UserInterface.php</file>-->
<!-- <file>src/DoctrineAuditBundle/User/UserProviderInterface.php</file>-->
<!-- </exclude>-->
</whitelist>
</filter>
</phpunit>
67 changes: 67 additions & 0 deletions src/Auditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace DH\Auditor;

use DH\Auditor\Provider\ProviderInterface;
use ReflectionException;
use ReflectionMethod;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Auditor
{
/**
* @var Configuration
*/
private $configuration;

/**
* @var ProviderInterface
*/
private $provider;

/**
* @var EventDispatcherInterface
*/
private $dispatcher;

/**
* @var bool
*/
private $is_pre43_dispatcher;

/**
* @throws ReflectionException
*/
public function __construct(Configuration $configuration, ProviderInterface $provider, EventDispatcherInterface $dispatcher)
{
$this->configuration = $configuration;
$this->provider = $provider;
$this->dispatcher = $dispatcher;

$this->provider->setAuditor($this);

$r = new ReflectionMethod($this->dispatcher, 'dispatch');
$p = $r->getParameters();
$this->is_pre43_dispatcher = 2 === \count($p) && 'event' !== $p[0]->name;
}

public function isPre43Dispatcher(): bool
{
return $this->is_pre43_dispatcher;
}

public function getConfiguration(): Configuration
{
return $this->configuration;
}

public function getProvider(): ProviderInterface
{
return $this->provider;
}

public function getEventDispatcher(): EventDispatcherInterface
{
return $this->dispatcher;
}
}
77 changes: 77 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace DH\Auditor;

use Symfony\Component\OptionsResolver\OptionsResolver;

class Configuration
{
/**
* @var bool
*/
private $enabled;

/**
* @var string
*/
private $timezone;

public function __construct(array $options)
{
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$config = $resolver->resolve($options);

$this->enabled = $config['enabled'];
$this->timezone = $config['timezone'];
}

public function configureOptions(OptionsResolver $resolver): void
{
// https://symfony.com/doc/current/components/options_resolver.html
$resolver
->setDefaults([
'enabled' => true,
'timezone' => 'UTC',
])
->setAllowedTypes('enabled', 'bool')
->setAllowedTypes('timezone', 'string')
;
}

/**
* enabled auditing.
*/
public function enable(): self
{
$this->enabled = true;

return $this;
}

/**
* disable auditing.
*/
public function disable(): self
{
$this->enabled = false;

return $this;
}

/**
* Is auditing enabled.
*/
public function isEnabled(): bool
{
return $this->enabled;
}

/**
* Get the value of timezone.
*/
public function getTimezone(): string
{
return $this->timezone;
}
}
Loading

0 comments on commit b022b11

Please sign in to comment.