Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use service as a person_fn #71

Closed
wants to merge 1 commit into from

Conversation

mrardon
Copy link

@mrardon mrardon commented Dec 1, 2021

Description of the change

This is a new feature to allow person_fn to use a Symfony service.

Usage

This worked for me with Symfony 5.4 I didn't test them on any other Symfony versions

  1. Create a Symfony service with an __invoke method that returns the person data array, using DI you can inject any service you want into the __construct method
<?php

namespace App;

use App\Entity\Person;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class RollbarUserInfo
{
    private ?UserInterface $user = null;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $token = $tokenStorage->getToken();

        if (null !== $token) {
            $this->user = $token->getUser();
        }
    }

    /**
     * @return array{
     *      id?: int,
     *      username?: string,
     *      email?: string
     * }
     */
    public function __invoke(): array
    {
        $user = $this->user;

        if ($user instanceof Person) {
            return [
                'id'       => $user->getId(),
                'username' => $user->getName(),
                'email'    => $user->getEmail(),
            ];
        }

        //You can just return an empty array here or implement the default serialize methods here again
        return [];
    }
}

  1. Define the service as public
services:
    App\RollbarUserInfo:
        public: true
  1. Configure bundle to use the new service
rollbar:
    person_service: App\RollbarUserInfo

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Maintenance
  • New release

Related issues

Checklists

Development

I could not get any of the tests to run because I could not get the dev dependencies installed.

  Problem 1
    - matthiasnoback/symfony-dependency-injection-test v1.1.0 requires symfony/dependency-injection ^2.3|^3.0 -> found symfony/dependency-injection[v2.3.0, ..., v2.8.52, v3.0.0, ..., v3.4.47] but it conflicts with your root composer.json require (^5.0).
    - matthiasnoback/symfony-dependency-injection-test v1.2.0 requires symfony/dependency-injection ^2.7 || ^3.3 || ^4.0 -> found symfony/dependency-injection[v2.7.0, ..., v2.8.52, v3.3.0, ..., v3.4.47, v4.0.0, ..., v4.4.34] but it conflicts with your root composer.json require (^5.0).
    - Root composer.json requires matthiasnoback/symfony-dependency-injection-test ^1.1 -> satisfiable by matthiasnoback/symfony-dependency-injection-test[v1.1.0, v1.2.0].
  • Lint rules pass locally
  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • "Ready for review" label attached to the PR and reviewers assigned
  • Issue from task tracker has a link to this pull request
  • Changes have been reviewed by at least one other engineer

@kwolniak
Copy link

kwolniak commented Mar 2, 2023

Guys help me, is there any way to just access logged in user in person_fn static function or is it just useless?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possibility to use service as person_fn rather than a static function
2 participants