<commit_before><?php

namespace Directorzone\Controller\Directory\Company;

use Netsensia\Controller\NetsensiaActionController;
use Directorzone\Service\CompanyService;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Directorzone\Service\TwitterService;
use Directorzone\Service\BingService;

class CompanyViewController extends NetsensiaActionController
{
    /**
     * @var CompanyService
     */
    private $companyService;
    
    /**
     * @var TwitterService
     */
    private $twitterService;
    
    /**
     * @var BingService
     */
    private $bingService;
    
    public function __construct(
        CompanyService $companyService,
        TwitterService $twitterService,
        BingService $bingService
    ) {
        $this->companyService = $companyService;
        $this->twitterService = $twitterService;
        $this->bingService = $bingService;
    }
    
    public function companyDetailsAction()
    {
        $companyDirectoryId = $this->params('id');
                
        try {
            
            $zendCache = $this->getServiceLocator()->get('ZendCache');
            
            $cacheKey = 'companyDetailsAction_' . $companyDirectoryId;
            
            $success = false;
            $result = $zendCache->getItem($cacheKey, $success);
            
            if ($success) {
                return $result;
            }
            
            $companyDetails = $this->companyService->getCompanyDetails(
                $companyDirectoryId
            );
            
            $twitterSearchTerm = str_replace('limited', '', strtolower($companyDetails['name']));
            $twitterSearchTerm = str_replace('ltd', '', $twitterSearchTerm);
            $twitterSearchTerm = str_replace('holdings', '', $twitterSearchTerm);
            $twitterSearchTerm = str_replace('plc', '', $twitterSearchTerm);
            $twitterSearchTerm = str_replace('&', 'and', $twitterSearchTerm);
            
            $twitterSearchTerm = trim($twitterSearchTerm);
            
            $twitterResults = $this->twitterService->search(
                $twitterSearchTerm,
                5
            );
            
            $bingSearchTerm = $twitterSearchTerm;
            
            $searchResults = [];
            
            $searchResults = $this->bingService->search(
	           $bingSearchTerm,
               4
            );
                        
            $returnArray = array_merge(
                $companyDetails,
                $twitterResults
            );
            
            if (isset($searchResults['d']['results'])) {
                $bingResults = ['bing' => $searchResults['d']['results']];
            } else {
                $bingResults = ['bing' => []];
            }
            
            $returnArray = array_merge(
                $returnArray,
                $bingResults
            );
            
            $zendCache->setItem($cacheKey, $returnArray);
            
            return $returnArray;
            
        } catch (NotFoundResourceException $e) {
            
            $this->getResponse()->setStatusCode(404);
            
        }
    }
}<commit_msg>Fix cache problem - only return feeds from cache<commit_after><?php

namespace Directorzone\Controller\Directory\Company;

use Netsensia\Controller\NetsensiaActionController;
use Directorzone\Service\CompanyService;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Directorzone\Service\TwitterService;
use Directorzone\Service\BingService;

class CompanyViewController extends NetsensiaActionController
{
    /**
     * @var CompanyService
     */
    private $companyService;
    
    /**
     * @var TwitterService
     */
    private $twitterService;
    
    /**
     * @var BingService
     */
    private $bingService;
    
    public function __construct(
        CompanyService $companyService,
        TwitterService $twitterService,
        BingService $bingService
    ) {
        $this->companyService = $companyService;
        $this->twitterService = $twitterService;
        $this->bingService = $bingService;
    }
    
    public function companyDetailsAction()
    {
        $companyDirectoryId = $this->params('id');
                
        try {
            
            $companyDetails = $this->companyService->getCompanyDetails(
                $companyDirectoryId
            );
            
            $zendCache = $this->getServiceLocator()->get('ZendCache');

            $cacheKey = 'companyDetailsActionFeedResults' . $companyDirectoryId;
            
            $success = false;
            $result = $zendCache->getItem($cacheKey, $success);
            
            if ($success) {
                $feedResults = $result;
            } else {
                $twitterSearchTerm = str_replace('limited', '', strtolower($companyDetails['name']));
                $twitterSearchTerm = str_replace('ltd', '', $twitterSearchTerm);
                $twitterSearchTerm = str_replace('holdings', '', $twitterSearchTerm);
                $twitterSearchTerm = str_replace('plc', '', $twitterSearchTerm);
                $twitterSearchTerm = str_replace('&', 'and', $twitterSearchTerm);
                
                $twitterSearchTerm = trim($twitterSearchTerm);
                
                $twitterResults = $this->twitterService->search(
                    $twitterSearchTerm,
                    5
                );
                
                $bingSearchTerm = $twitterSearchTerm;
                
                $searchResults = [];
                
                $searchResults = $this->bingService->search(
                    $bingSearchTerm,
                    4
                );
                
                $returnArray = array_merge(
                    $companyDetails,
                    $twitterResults
                );
                
                if (isset($searchResults['d']['results'])) {
                    $bingResults = ['bing' => $searchResults['d']['results']];
                } else {
                    $bingResults = ['bing' => []];
                }
                
                $feedResults = array_merge(
                    $twitterResults,
                    $bingResults
                );
                
                $zendCache->setItem($cacheKey, $feedResults);
            }
               
            $returnArray = array_merge(
                $companyDetails,
                $feedResults
            );
            
            return $returnArray;
            
        } catch (NotFoundResourceException $e) {
            
            $this->getResponse()->setStatusCode(404);
            
        }
    }
}
{'fraction_non_alphanumeric': 0.10405565423327413, 'fraction_numerical': 0.0014801657785671995, 'mean_word_length': 1.5250373692077728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1094194', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1570, 'n_words': 252}