Skip to content

Commit

Permalink
Merge pull request #15 from OpenClassrooms/ADD_LOG
Browse files Browse the repository at this point in the history
Add log 🐼
  • Loading branch information
Ptidel authored and Ptidel committed Mar 24, 2015
2 parents f9bdbc5 + 471dd0d commit 25e9bbb
Show file tree
Hide file tree
Showing 95 changed files with 1,600 additions and 299 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
composer.lock
composer.phar
vendor/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- "5.3"
- "5.4"
- "5.5"
- "5.6"
Expand Down
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ UseCase is a library that provides facilities to manage technical code over a Us
- Cache management
- Transactional context
- Events
- Logs

The goal is to have only functional code on the Use Case and manage technical code in an elegant way using annotations.

Expand Down Expand Up @@ -80,6 +81,11 @@ class app()
* @var OpenClassrooms\UseCase\Application\Services\EventSender\EventFactory
*/
private $eventFactory;

/**
* @var Psr\Log\LoggerInterface
*/
private $logger;

/**
* @var Doctrine\Common\Annotations\Reader
Expand All @@ -94,8 +100,9 @@ class app()
->withSecurity($this->security)
->withCache($this->cache)
->withTransaction($this->transaction)
->withEvent($this->event)
->withEventSender($this->event)
->withEventFactory($this->eventFactory)
->withLogger($this->logger)
->build();
}
}
Expand Down Expand Up @@ -215,7 +222,6 @@ Other options:
- commit
- rollback on exception

Will use the previous active transaction if there is one.

```php

Expand Down Expand Up @@ -294,13 +300,78 @@ Prefixes can be :
*
* @Event(methods="pre, post, onException")
* Send an event before the call of UseCase->execute(), after the call of UseCase->execute() or on exception
*
* @Event(name="first_event")
* @Event(name="second_event")
* Send two events
*/
```

### Log

@Log annotation allows to add log following the [PSR standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md).


```php
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase;
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCaseRequest;
use OpenClassrooms\UseCase\BusinessRules\Responders\UseCaseResponse;
use OpenClassrooms\UseCase\Application\Annotations\Log;

class AUseCase implements UseCase
{
/**
* @Log
*
* @return UseCaseResponse
*/
public function execute(UseCaseRequest $useCaseRequest)
{
// do things

return $useCaseResponse;
}
}
```

The log can be:
- pre execute
- post execute
- on exception
or all of them.

On exception is default.

Level can be specified following [PSR's levels](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel).
Warning is default.


```php
/**
* @Log(level="error")
* Log with the level 'error'
*
* @Log (message="message with context {foo}", context={"foo":"bar"})
* Log with standard message
*
* @Log(methods="pre")
* Log before the call of UseCase->execute()
*
* @Log(methods="pre, post, onException")
* Log before the call of UseCase->execute(), after the call of UseCase->execute() or on exception
*
* @Log(methods="pre", level="debug")
* @Log(methods="onException", level="error")
* Log before the call of UseCase->execute() with debug level and on exception with error level
*/
```


### Workflow
The execution order is the following:

Pre Excecute:
- log (pre)
- security
- cache (fetch)
- transaction (begin transaction)
Expand All @@ -310,8 +381,12 @@ Post Excecute:
- cache (save if needed)
- transaction (commit)
- event (post)
- log (post)

On Exception:
- log (on exception)
- transaction (rollback)
- event (on exception)

### Utils
The library provide a generic response for paginated collection.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"name": "OpenClassrooms",
"email": "technique@openclassrooms.com"
"email": "tech@openclassrooms.com"
}
],
"autoload": {
Expand All @@ -25,11 +25,12 @@
}
},
"require": {
"php": ">=5.4",
"doctrine/annotations": "1.*",
"openclassrooms/cache": "1.0.*@dev"
},
"require-dev": {
"phpunit/phpunit": "~3.7",
"phpunit/phpunit": "~4.4",
"satooshi/php-coveralls": "dev-master"
},
"extra": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
class Cache
{

/**
* @var string
*/
Expand Down
4 changes: 3 additions & 1 deletion src/OpenClassrooms/UseCase/Application/Annotations/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function __construct(array $values)

foreach ($this->methods as $method) {
if (!in_array($method, self::$allowedMethods)) {
throw new \InvalidArgumentException('Method "'.$method.'" is not allowed. Allowed: pre, post and onException');
throw new \InvalidArgumentException(
'Method "'.$method.'" is not allowed. Allowed: pre, post and onException'
);
}
}
}
Expand Down
132 changes: 132 additions & 0 deletions src/OpenClassrooms/UseCase/Application/Annotations/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace OpenClassrooms\UseCase\Application\Annotations;

use Psr\Log\LogLevel;

/**
* @author Romain Kuzniak <romain.kuzniak@turn-it-up.org>
* @Annotation
*/
class Log
{
const PRE_METHOD = 'pre';

const POST_METHOD = 'post';

const ON_EXCEPTION_METHOD = 'onException';

const DEFAULT_METHOD = self::ON_EXCEPTION_METHOD;

const DEFAULT_LEVEL = LogLevel::WARNING;

/**
* @var string[]
*/
private static $allowedMethods = array(
self::PRE_METHOD,
self::POST_METHOD,
self::ON_EXCEPTION_METHOD,
);

/**
* @var string[]
*/
private static $allowedLevels = array(
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::DEBUG,
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
);

/**
* @var string
*/
public $level = self::DEFAULT_LEVEL;

/**
* @var string
*/
public $message;

/**
* @var array
*/
public $context = array();

/**
* @var string[]
*/
public $methods = array(self::DEFAULT_METHOD);

public function __construct(array $values)
{
if (isset($values['level'])) {
$this->level = $values['level'];
if (!in_array($this->level, self::$allowedLevels)) {
throw new \InvalidArgumentException(
'Level "'.$this->level.'" is not a valid PSR level. See Psr\Log\LogLevel.'
);
}
}

if (isset($values['methods'])) {
$this->methods = is_array($values['methods']) ? $values['methods'] :
array_map('trim', explode(',', $values['methods']));
}

foreach ($this->methods as $method) {
if (!in_array($method, self::$allowedMethods)) {
throw new \InvalidArgumentException(
'Method "'.$method.'" is not allowed. Allowed: pre, post and onException'
);
}
}

if (isset($values['message'])) {
$this->message = $values['message'];
}

if (isset($values['context'])) {
if (is_array($values['context'])) {
$this->context = $values['context'];
}
}
}

/**
* @return string[]
*/
public function getMethods()
{
return $this->methods;
}

/**
* @return string
*/
public function getLevel()
{
return $this->level;
}

/**
* @return array
*/
public function getContext()
{
return $this->context;
}

/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
class Security
{

/**
* @var mixed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
class CacheProxyStrategy implements PreExecuteProxyStrategy, PostExecuteProxyStrategy
{

/**
* @var mixed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
class CacheProxyStrategyRequestBuilderImpl implements CacheProxyStrategyRequestBuilder
{

/**
* @var CacheProxyStrategyRequestDTO
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
class CacheProxyStrategyRequestDTO implements CacheProxyStrategyRequest
{

/**
* @var int
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
class CacheProxyStrategyBagImpl extends ProxyStrategyBag
{

/**
* @var CacheProxyStrategy
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
class ProxyStrategyResponseDTO implements ProxyStrategyResponse
{

/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
class EventProxyStrategyRequestBuilderImpl implements EventProxyStrategyRequestBuilder
{

/**
* @var EventProxyStrategyRequestDTO
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
class EventProxyStrategyRequestDTO implements EventProxyStrategyRequest
{

/**
* @var string
*/
Expand Down

0 comments on commit 25e9bbb

Please sign in to comment.