Skip to content

Commit 92e714c

Browse files
author
Andrii Afanasiev
committed
issue #3: all to track person data; small update for issue #4;
1 parent ae394b6 commit 92e714c

19 files changed

+482
-15
lines changed

DependencyInjection/SymfonyRollbarExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SymfonyRollbarExtension extends Extension
2121
* @param array $configs An array of configuration values
2222
* @param ContainerBuilder $container A ContainerBuilder instance
2323
*
24-
* @throws \InvalidArgumentException When provided tag is not defined in this extension
24+
* @throws \Exception
2525
*/
2626
public function load(array $configs, ContainerBuilder $container)
2727
{
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Provider;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use SymfonyRollbarBundle\Provider\PersonInterface;
7+
8+
abstract class AbstractPersonProvider
9+
{
10+
/**
11+
* @var \SymfonyRollbarBundle\Provider\PersonInterface
12+
*/
13+
protected $person;
14+
15+
/**
16+
* Initialize current person that should be tracked with Rollbar
17+
*
18+
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
19+
*/
20+
abstract public function __construct(ContainerInterface $container);
21+
22+
/**
23+
* @return array|null
24+
*/
25+
public function getPerson()
26+
{
27+
if (empty($this->person)) {
28+
return null;
29+
}
30+
31+
return [
32+
'id' => $this->person->getId(),
33+
'username' => $this->person->getUsername(),
34+
'email' => $this->person->getEmail(),
35+
];
36+
}
37+
38+
/**
39+
* @param \SymfonyRollbarBundle\Provider\PersonInterface $person
40+
*
41+
* @return AbstractPersonProvider
42+
*/
43+
public function setPerson(PersonInterface $person = null)
44+
{
45+
$this->person = $person;
46+
47+
return $this;
48+
}
49+
}

Provider/ApiClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function __construct(ContainerInterface $container)
3838
{
3939
$this->container = $container;
4040

41-
// There is no API in Rollbar SDK for tracking builds
4241
$config = $this->container->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
4342
$this->endpoint = $config['rollbar']['base_api_url'];
4443

@@ -57,8 +56,10 @@ public function __construct(ContainerInterface $container)
5756
*/
5857
public function trackBuild($payload = [])
5958
{
60-
return $this->client->post('deploy', [
59+
$rsp = $this->client->post('deploy', [
6160
'form_params' => $payload,
6261
]);
62+
63+
return $rsp;
6364
}
6465
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Provider;
4+
5+
/**
6+
* Interface InterfacePersonProvider
7+
*
8+
* @package SymfonyRollbarBundle\Provider
9+
*/
10+
interface InterfacePersonProvider
11+
{
12+
/**
13+
* @return array|null
14+
*/
15+
public function getPerson();
16+
}

Provider/PersonInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Provider;
4+
5+
/**
6+
* Interface PersonInterface
7+
*
8+
* @package SymfonyRollbarBundle\Provider
9+
* @link https://rollbar.com/docs/person-tracking/
10+
*/
11+
interface PersonInterface
12+
{
13+
/**
14+
* @return string
15+
*/
16+
public function getId();
17+
18+
/**
19+
* @return string
20+
*/
21+
public function getUsername();
22+
23+
/**
24+
* @return string
25+
*/
26+
public function getEmail();
27+
}

Provider/RollbarHandler.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class RollbarHandler extends AbstractProcessingHandler
3434
*/
3535
protected $exclude;
3636

37+
/**
38+
* @var array
39+
*/
40+
protected $rbConfig;
41+
3742
/**
3843
* Monolog vs Rollbar
3944
* @var array
@@ -61,10 +66,25 @@ public function __construct(ContainerInterface $container, $level = Logger::ERRO
6166
$this->container = $container;
6267
parent::__construct($level, $bubble);
6368

64-
// init notifier
65-
$config = $this->getContainer()->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
66-
$kernel = $container->get('kernel');
69+
$this->rbConfig = $this->initialize();
6770

71+
if (!empty($this->rbConfig)) {
72+
RollbarNotifier::init($this->rbConfig, false, false, false);
73+
}
74+
}
75+
76+
/**
77+
* @return array
78+
*/
79+
protected function initialize()
80+
{
81+
try {
82+
$config = $this->getContainer()->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
83+
} catch (\Exception $e) {
84+
return null;
85+
}
86+
87+
$kernel = $this->container->get('kernel');
6888
$rConfig = $config['rollbar'];
6989
$override = [
7090
'root' => $kernel->getRootDir(),
@@ -75,9 +95,24 @@ public function __construct(ContainerInterface $container, $level = Logger::ERRO
7595
$rConfig[$key] = $value;
7696
}
7797

98+
// DI for 'person_fn'
99+
if (!empty($rConfig['person_fn'])) {
100+
if ($this->container->has($rConfig['person_fn'])) {
101+
$service = $this->container->get($rConfig['person_fn']);
102+
} elseif (class_exists($rConfig['person_fn'])) {
103+
$service = new $rConfig['person_fn']($this->container);
104+
}
105+
106+
if (!empty($service) && $service instanceof AbstractPersonProvider) {
107+
$rConfig['person_fn'] = [$service, 'getPerson'];
108+
} else {
109+
$rConfig['person_fn'] = is_callable($rConfig['person_fn']) ? $rConfig['person_fn'] : null;
110+
}
111+
}
112+
78113
$this->exclude = empty($config['exclude']) ? [] : $config['exclude'];
79114

80-
RollbarNotifier::init($rConfig, false, false, false);
115+
return $rConfig;
81116
}
82117

83118
/**
@@ -93,6 +128,10 @@ public function getContainer()
93128
*/
94129
protected function write(array $record)
95130
{
131+
if (empty($this->rbConfig)) {
132+
return;
133+
}
134+
96135
if (!$this->initialized) {
97136
// __destructor() doesn't get called on Fatal errors
98137
register_shutdown_function([$this, 'close']);
@@ -179,9 +218,7 @@ public function close()
179218
public function trackBuild($environment, $revision, $comment = '', $rollbarUser = '', $localUser = '')
180219
{
181220
// There is no API in Rollbar SDK for tracking builds
182-
$config = $this->getContainer()->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
183-
184-
if (!$config['enable']) {
221+
if (empty($this->rbConfig)) {
185222
return null;
186223
}
187224

@@ -190,7 +227,7 @@ public function trackBuild($environment, $revision, $comment = '', $rollbarUser
190227

191228
// truncate payload according to limits
192229
$payload = [
193-
'access_token' => $config['rollbar']['access_token'],
230+
'access_token' => $this->rbConfig['access_token'],
194231
'environment' => Filter::process($environment, Filter\Length::class),
195232
'revision' => Filter::process($revision, Filter\Length::class),
196233
// @link https://stackoverflow.com/questions/4420164/how-much-utf-8-text-fits-in-a-mysql-text-field

Resources/doc/configuration.rst

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ in `official documentation`_ for Rollbar PHP lib.
6060

6161
.. _`official documentation`: https://rollbar.com/docs/notifier/rollbar-php/
6262

63-
RollBar settings
63+
RollBar - Settings
6464
--------------------
6565

6666
Here you can description of some important configuration options for RollBar.
@@ -72,3 +72,53 @@ Here you can description of some important configuration options for RollBar.
7272
``environment``: Environment name, e.g. 'production' or 'development'. Default: ``production``
7373

7474
``root``: Path to your project's root dir. Default ``%kernel.root_dir%``
75+
76+
77+
RollBar - Person Tracking
78+
--------------------
79+
Rollbar `can track`_ which of your People (users) are affected by each error. There is one of the options:
80+
81+
``person_fn``: A function reference (string, etc. - anything that `call_user_func()`_ can handle) returning an array like the one for 'person'.
82+
83+
Use globally defined function:
84+
85+
.. code-block:: yaml
86+
87+
symfony_rollbar:
88+
# ...
89+
rollbar:
90+
# ...
91+
person_fn: 'function_name_here'
92+
93+
Use custom ``PersonProvider`` class that should implements ``InterfacePersonProvider``:
94+
95+
.. code-block:: yaml
96+
97+
symfony_rollbar:
98+
# ...
99+
rollbar:
100+
# ...
101+
person_fn: '\SymfonyRollbarBundle\Tests\Fixtures\PersonProvider'
102+
103+
Use custom ``PersonProvider`` service that class should implements ``InterfacePersonProvider``:
104+
105+
.. code-block:: yaml
106+
107+
symfony_rollbar:
108+
# ...
109+
rollbar:
110+
# ...
111+
person_fn: 'awesome_app.rollbar_person_provider'
112+
113+
Than in your ``PersonProvider`` class/service or function you have to return user data as array:
114+
115+
.. code-block:: php
116+
// ..
117+
return [
118+
'id' => 'user_id',
119+
'username' => 'username',
120+
'email' => 'email',
121+
];
122+
123+
.. _`can track`: https://rollbar.com/docs/person-tracking/
124+
.. _`call_user_func()`: http://php.net/call_user_func

Tests/Fixtures/AwesomePerson.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Tests\Fixtures;
4+
5+
use SymfonyRollbarBundle\Provider\PersonInterface;
6+
7+
/**
8+
* Class Person
9+
*
10+
* @package SymfonyRollbarBundle\Provider
11+
*/
12+
class AwesomePerson implements PersonInterface
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $id;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $username;
23+
24+
/**
25+
* @var string
26+
*/
27+
protected $email;
28+
29+
/**
30+
* Person constructor.
31+
*
32+
* @param string $id
33+
* @param string $username
34+
* @param string $email
35+
*/
36+
public function __construct($id, $username = null, $email = null)
37+
{
38+
$this->id = (string)$id;
39+
$this->username = (string)$username;
40+
$this->email = (string)$email;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getId()
47+
{
48+
return $this->id;
49+
}
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getUsername()
55+
{
56+
return $this->username;
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
public function getEmail()
63+
{
64+
return $this->email;
65+
}
66+
}

Tests/Fixtures/PersonProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use SymfonyRollbarBundle\Provider\AbstractPersonProvider;
7+
8+
class PersonProvider extends AbstractPersonProvider
9+
{
10+
/**
11+
* Initialize current person that should be tracked with Rollbar
12+
*
13+
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
14+
*/
15+
public function __construct(ContainerInterface $container)
16+
{
17+
}
18+
}

Tests/Fixtures/app/AppKernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public function registerContainerConfiguration(LoaderInterface $loader)
3939
*/
4040
public function getCacheDir()
4141
{
42-
return sys_get_temp_dir() . '/var/' . $this->environment . '/cache';
42+
return sys_get_temp_dir() . '/var/' . $this->getEnvironment() . '/cache';
4343
}
4444

4545
public function getLogDir()
4646
{
47-
return sys_get_temp_dir() . '/var/' . $this->environment . '/logs';
47+
return sys_get_temp_dir() . '/var/' . $this->getEnvironment() . '/logs';
4848
}
4949
}

0 commit comments

Comments
 (0)