8000 feature #37830 [Notifier] Add LinkedIn provider (ismail1432) · jeremyFreeAgent/symfony@14c9d05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14c9d05

Browse files
committed
feature symfony#37830 [Notifier] Add LinkedIn provider (ismail1432)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Notifier] Add LinkedIn provider | Q | A | ------------- | --- | Branch? | master for features 5.2 | Bug fix? | no | New feature? | yes/no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | yes/no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix symfony#34563 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> ### Add the LinkedIn provider for the Notifier Few months ago I created a bridge to send message to LinkedIn, following [the discussion here](symfony#34563) I create the PR. If you're curious [I wrote an article where I use the bridge](https://medium.com/@smaine.milianni/aws-lambda-and-symfony-6d3e9831c3cd) [Edit] test are green ~missing improvement body construction and integration test with update changes + framework integration~ [Notification sent with this PR](https://www.linkedin.com/posts/smainemilianni_hello-linkedin-from-symfony-bridge-see-pull-activity-6700328970665177088-31tT) <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch master. --> Commits ------- 0064cae add Linkedin transport and option
2 parents 830cc5b + 0064cae commit 14c9d05

15 files changed

+927
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
18+
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
@@ -38,6 +39,10 @@
3839
->parent('notifier.transport_factory.abstract')
3940
->tag('chatter.transport_factory')
4041

42+
->set('notifier.transport_factory.linkedin', LinkedInTransportFactory::class)
43+
->parent('notifier.transport_factory.abstract')
44+
->tag('chatter.transport_factory')
45+
4146
->set('notifier.transport_factory.telegram', TelegramTransportFactory::class)
4247
->parent('notifier.transport_factory.abstract')
4348
->tag('chatter.transport_factory')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.2.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LinkedIn;
13+
14+
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\AuthorShare;
15+
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\LifecycleStateShare;
16+
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\ShareContentShare;
17+
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\VisibilityShare;
18+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
19+
use Symfony\Component\Notifier\Notification\Notification;
20+
21+
/**
22+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
23+
*
24+
* @experimental in 5.2
25+
*/
26+
final class LinkedInOptions implements MessageOptionsInterface
27+
{
28+
private $options = [];
29+
30+
public function __construct(array $options = [])
31+
{
32+
$this->options = $options;
33+
}
34+
35+
public function toArray(): array
36+
{
37+
return $this->options;
38+
}
39+
40+
public function getRecipientId(): ?string
41+
{
42+
return null;
43+
}
44+
45+
public static function fromNotification(Notification $notification): self
46+
{
47+
$options = new self();
48+
$options->specificContent(new ShareContentShare($notification->getSubject()));
49+
50+
if ($notification->getContent()) {
51+
$options->specificContent(new ShareContentShare($notification->getContent()));
52+
}
53+
54+
$options->visibility(new VisibilityShare());
55+
$options->lifecycleState(new LifecycleStateShare());
56+
57+
return $options;
58+
}
59+
60+
public function contentCertificationRecord(string $contentCertificationRecord): self
61+
{
62+
$this->options['contentCertificationRecord'] = $contentCertificationRecord;
63+
64+
return $this;
65+
}
66+
67+
public function firstPublishedAt(int $firstPublishedAt): self
68+
{
69+
$this->options['firstPublishedAt'] = $firstPublishedAt;
70+
71+
return $this;
72+
}
73+
74+
public function lifecycleState(LifecycleStateShare $lifecycleStateOption): self
75+
{
76+
$this->options['lifecycleState'] = $lifecycleStateOption->lifecycleState();
77+
78+
return $this;
79+
}
80+
81+
public function origin(string $origin): self
82+
{
83+
$this->options['origin'] = $origin;
84+
85+
return $this;
86+
}
87+
88+
public function ugcOrigin(string $ugcOrigin): self
89+
{
90+
$this->options['ugcOrigin'] = $ugcOrigin;
91+
92+
return $this;
93+
}
94+
95+
public function versionTag(string $versionTag): self
96+
{
97+
$this->options['versionTag'] = $versionTag;
98+
99+
return $this;
100+
}
101+
102+
public function specificContent(ShareContentShare $specificContent): self
103+
{
104+
$this->options['specificContent']['com.linkedin.ugc.ShareContent'] = $specificContent->toArray();
105+
106+
return $this;
107+
}
108+
109+
public function author(AuthorShare $authorOption): self
110+
{
111+
$this->options['author'] = $authorOption->author();
112+
113+
return $this;
114+
}
115+
116+
public function visibility(VisibilityShare $visibilityOption): self
117+
{
118+
$this->options['visibility'] = $visibilityOption->toArray();
119+
120+
return $this;
121+
}
122+
123+
public function getAuthor(): ?string
124+
{
125+
return $this->options['author'] ?? null;
126+
}
127+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LinkedIn;
13+
14+
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\AuthorShare;
15+
use Symfony\Component\Notifier\Exception\LogicException;
16+
use Symfony\Component\Notifier\Exception\TransportException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
26+
*
27+
* @experimental in 5.2
28+
*
29+
* @see https://docs.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api#sharecontent
30+
*/
31+
final class LinkedInTransport extends AbstractTransport
32+
{
33+
protected const PROTOCOL_VERSION = '2.0.0';
34+
protected const PROTOCOL_HEADER = 'X-Restli-Protocol-Version';
35+
protected const HOST = 'api.linkedin.com';
36+
37+
private $authToken;
38+
private $accountId;
39+
40+
public function __construct(string $authToken, string $accountId, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
41+
{
42+
$this->authToken = $authToken;
43+
$this->accountId = $accountId;
44+
45+
parent::__construct($client, $dispatcher);
46+
}
47+
48+
public function __toString(): string
49+
{
50+
return sprintf('linkedin://%s', $this->getEndpoint());
51+
}
52+
53+
public function supports(MessageInterface $message): bool
54+
{
55+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof LinkedInOptions);
56+
}
57+
58+
/**
59+
* @see https://docs.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api
60+
*/
61+
protected function doSend(MessageInterface $message): SentMessage
62+
{
63+
if (!$message instanceof ChatMessage) {
64+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
65+
}
66+
if ($message->getOptions() && !$message->getOptions() instanceof LinkedInOptions) {
67+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, LinkedInOptions::class));
68+
}
69+
70+
if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
71+
$opts = LinkedInOptions::fromNotification($notification);
72+
$opts->author(new AuthorShare($this->accountId));
73+
}
74+
75+
$endpoint = sprintf('https://%s/v2/ugcPosts', $this->getEndpoint());
76+
77+
$response = $this->client->request('POST', $endpoint, [
78+
'auth_bearer' => $this->authToken,
79+
'headers' => [self::PROTOCOL_HEADER => self::PROTOCOL_VERSION],
80+
'json' => array_filter($opts ? $opts->toArray() : $this->bodyFromMessageWithNoOption($message)),
81+
]);
82+
83+
if (201 !== $response->getStatusCode()) {
84+
throw new TransportException(sprintf('Unable to post the Linkedin message: "%s".', $response->getContent(false)), $response);
85+
}
86+
87+
$result = $response->toArray(false);
88+
89+
if (!$result['id']) {
90+
throw new TransportException(sprintf('Unable to post the Linkedin message : "%s".', $result['error']), $response);
91+
}
92+
93+
return new SentMessage($message, (string) $this);
94+
}
95+
96+
private function bodyFromMessageWithNoOption(MessageInterface $message): array
97+
{
98+
return [
99+
'specificContent' => [
100+
'com.linkedin.ugc.ShareContent' => [
101+
'shareCommentary' => [
102+
'attributes' => [],
103+
'text' => $message->getSubject(),
104+
],
105+
'shareMediaCategory' => 'NONE',
106+
],
107+
],
108+
'visibility' => [
109+
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
110+
],
111+
'lifecycleState' => 'PUBLISHED',
112+
'author' => sprintf('urn:li:person:%s', $this->accountId),
113+
];
114+
}
115+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LinkedIn;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
class LinkedInTransportFactory extends AbstractTransportFactory
25+
{
26+
public function create(Dsn $dsn): TransportInterface
27+
{
28+
$scheme = $dsn->getScheme();
29+
$authToken = $this->getUser($dsn);
30+
$accountId = $this->getPassword($dsn);
31+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
32+
$port = $dsn->getPort();
33+
34+
if ('linkedin' === $scheme) {
35+
return (new LinkedInTransport($authToken, $accountId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
throw new UnsupportedSchemeException($dsn, 'linkedin', $this->getSupportedSchemes());
39+
}
40+
41+
protected function getSupportedSchemes(): array
42+
{
43+
return ['linkedin'];
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
LinkedIn Notifier
2+
=================
3+
4+
Provides LinkedIn integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
// .env file
11+
LINKEDIN_DSN='linkedin://ACCESS_TOKEN:USER_ID@default'
12+
```
13+
14+
Resources
15+
---------
16+
17+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
18+
* [Report issues](https://github.com/symfony/symfony/issues) and
19+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
20+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LinkedIn\Share;
13+
14+
/**
15+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
16+
*
17+
* @experimental in 5.2
18+
*/
19+
abstract class AbstractLinkedInShare
20+
{
21+
protected $options = [];
22+
23+
public function toArray(): array
24+
{
25+
return $this->options;
26+
}
27+
}

0 commit comments

Comments
 (0)
0