1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
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 Core23\FacebookBundle\Block\Service; |
13
|
|
|
|
14
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
15
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
16
|
|
|
use Sonata\BlockBundle\Form\Mapper\FormMapper; |
17
|
|
|
use Sonata\BlockBundle\Meta\Metadata; |
18
|
|
|
use Sonata\BlockBundle\Meta\MetadataInterface; |
19
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
20
|
|
|
use Sonata\Form\Type\ImmutableArrayType; |
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
22
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
25
|
|
|
|
26
|
|
|
final class PageFeedBlockService extends AbstractFacebookBlockService |
27
|
|
|
{ |
28
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
29
|
|
|
{ |
30
|
|
|
$parameters = [ |
31
|
|
|
'context' => $blockContext, |
32
|
|
|
'settings' => $blockContext->getSettings(), |
33
|
|
|
'block' => $blockContext->getBlock(), |
34
|
|
|
'feed' => $this->getData($blockContext->getSettings()), |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
return $this->renderResponse($blockContext->getTemplate(), $parameters, $response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function configureEditForm(FormMapper $formMapper, BlockInterface $block): void |
41
|
|
|
{ |
42
|
|
|
$formMapper->add('settings', ImmutableArrayType::class, [ |
43
|
|
|
'keys' => [ |
44
|
|
|
['title', TextType::class, [ |
45
|
|
|
'label' => 'form.label_title', |
46
|
|
|
'required' => false, |
47
|
|
|
]], |
48
|
|
|
['translation_domain', TextType::class, [ |
49
|
|
|
'label' => 'form.label_translation_domain', |
50
|
|
|
'required' => false, |
51
|
|
|
]], |
52
|
|
|
['icon', TextType::class, [ |
53
|
|
|
'label' => 'form.label_icon', |
54
|
|
|
'required' => false, |
55
|
|
|
]], |
56
|
|
|
['class', TextType::class, [ |
57
|
|
|
'label' => 'form.label_class', |
58
|
|
|
'required' => false, |
59
|
|
|
]], |
60
|
|
|
['id', TextType::class, [ |
61
|
|
|
'label' => 'form.label_id', |
62
|
|
|
'required' => true, |
63
|
|
|
]], |
64
|
|
|
['limit', NumberType::class, [ |
65
|
|
|
'label' => 'form.label_limit', |
66
|
|
|
'required' => false, |
67
|
|
|
]], |
68
|
|
|
], |
69
|
|
|
'translation_domain' => 'Core23FacebookBundle', |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
74
|
|
|
{ |
75
|
|
|
$resolver->setDefaults([ |
76
|
|
|
'title' => null, |
77
|
|
|
'translation_domain' => null, |
78
|
|
|
'icon' => 'fa fa-facebook-official', |
79
|
|
|
'class' => null, |
80
|
|
|
'id' => null, |
81
|
|
|
'limit' => 10, |
82
|
|
|
'fields' => 'type,message,description,permalink_url,picture,created_time', |
83
|
|
|
'template' => '@Core23Facebook/Block/block_page_feed.html.twig', |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$resolver->setRequired(['id']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getMetadata(): MetadataInterface |
90
|
|
|
{ |
91
|
|
|
return new Metadata('core23_facebook.block.page_feed', null, null, 'Core23FacebookBundle', [ |
92
|
|
|
'class' => 'fa fa-facebook-official', |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function getData(array $settings): array |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$accessToken = $this->getAccessToken(); |
100
|
|
|
|
101
|
|
|
$response = $this->getFacebook()->get('/'.$settings['id'].'/feed?fields='.$settings['fields'], $accessToken); |
102
|
|
|
|
103
|
|
|
return $response->getGraphEdge()->asArray(); |
104
|
|
|
} catch (FacebookSDKException $exception) { |
105
|
|
|
$this->logger->warning(sprintf('Facebook SDK Exception: %s', $exception->getMessage())); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|