Skip to content

Instantly share code, notes, and snippets.

@YasuhiroYoshida
Last active April 14, 2021 12:24
Show Gist options
  • Save YasuhiroYoshida/3126571 to your computer and use it in GitHub Desktop.
Save YasuhiroYoshida/3126571 to your computer and use it in GitHub Desktop.
<?php
// src/Acme/TweetBundle/Controller/DefaultController.php
namespace Acme\TweetBundle\Controller;
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Themattharris\TmhOAuth;
use Acme\TweetBundle\Entity\SearchWord;
use Acme\TweetBundle\Entity\Makeover;
use Acme\TweetBundle\Form\Type\TweetSearchType;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function __construct()
{
// Obtain access to an OAuth 1.0A library written by @themattharris
// require_once "Themattharris/TmhOAuth.php";
}
public function indexAction(Request $request)
{
// Create an entity that is going to take care of a search word
$searchWord = new SearchWord();
$searchWord->setSearchWord('Enter a search word');
// Create a form object
$form = $this->createForm(new TweetSearchType(), $searchWord);
// This will eventually refers to fetched tweets
$tweets = array();
// When a search word is submitted, it's a POST method that is used
// And when proven valid, it will trigger communication with Twitter Search API
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// Obtain access to an OAuth 1.0A library written by @themattharris
// Send a query upon successful authentication
$twitter = new TmhOAuth(array("host" => "search.twitter.com"));
$twitter->request("GET", $twitter->url("search"),
array("q" => $form->get('search_word')->getData(),));
// Receive + decode results from Twitter
$results = json_decode($twitter->response["response"])->{"results"};
// Have the results processed so that the representation will be more suitable for the view
$makeover = new Makeover();
$tweets = $makeover->stackTweets($results);
}
}
// Give the representation to the View
return $this->render(
'AcmeTweetBundle:Default:index.html.twig',
array('form' => $form->createView(), 'tweets' => $tweets,));
}
}
@YasuhiroYoshida
Copy link
Author

I completely forgot why I put this code here. Possibly, the repo was private back then, but I was asked to show part of it to somebody outside...🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment