capitalise/companies-house-bundle
Installation
composer require capitalise/companies-house-bundle
Add the bundle to config/bundles.php:
return [
// ...
Capitalise\CompaniesHouseBundle\CompaniesHouseBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console capitalise:companies-house:install
Update .env with your Companies House API key:
COMPANIES_HOUSE_API_KEY=your_api_key_here
First Use Case Fetch a company profile by number:
use Capitalise\CompaniesHouseBundle\Service\CompaniesHouseService;
class CompanyController extends AbstractController
{
public function show(CompaniesHouseService $service, string $companyNumber)
{
$company = $service->getCompanyProfile($companyNumber);
return $this->json($company);
}
}
Route it:
# config/routes.yaml
company_profile:
path: /company/{number}
controller: App\Controller\CompanyController::show
Company Data Retrieval
$profile = $service->getCompanyProfile('09725555');
$companies = $service->searchCompanies(['company_name' => 'Acme']);
API Rate Limiting Handle throttling gracefully:
try {
$data = $service->getCompanyAccounts('09725555');
} catch (RateLimitExceededException $e) {
$this->addFlash('error', 'API rate limit exceeded. Retry later.');
return $this->redirectToRoute('home');
}
Data Transformation Use DTOs or custom mappings:
$mapper = new CompanyProfileMapper();
$dto = $mapper->map($service->getCompanyProfile('09725555'));
Caching Responses
Cache API responses for 5–10 minutes (respecting cache-control headers):
$cacheKey = 'ch_'.$companyNumber;
if (!$company = $this->cache->get($cacheKey)) {
$company = $service->getCompanyProfile($companyNumber);
$this->cache->set($cacheKey, $company, 600);
}
Event-Driven Updates
Subscribe to CompaniesHouseEvents for real-time updates:
$dispatcher->addListener(CompanyProfileUpdatedEvent::class, function ($event) {
// Sync with your database or trigger notifications
});
Batch Processing
Use getCompanyAccounts() with pagination:
$accounts = $service->getCompanyAccounts('09725555', ['items_per_page' => 100]);
API Key Management
.env and restrict access via Symfony’s env() function.Rate Limits
429 Too Many Requests.$service->setRateLimitHandler(new ExponentialBackoffHandler());
Data Freshness
company-status) return stale data. Use as_at parameter for historical accuracy:
$service->getCompanyProfile('09725555', ['as_at' => '2023-01-01']);
Error Handling
null or throw NotFoundException. Normalize responses:
$company = $service->getCompanyProfile($number) ?? throw new CompanyNotFoundException();
Enable API Logging
Configure Monolog in config/packages/monolog.yaml:
handlers:
companies_house:
type: stream
path: "%kernel.logs_dir%/companies_house.log"
level: debug
channels: ["companies_house"]
Validate Requests
Use CompaniesHouseValidator to check API responses:
$validator = new CompaniesHouseValidator();
$errors = $validator->validateCompanyProfile($response);
Custom Endpoints
Extend CompaniesHouseClient to add unsupported endpoints:
class CustomCompaniesHouseClient extends CompaniesHouseClient
{
public function getCustomData(string $endpoint, array $params = [])
{
return $this->request('GET', $endpoint, $params);
}
}
Response Transformers
Override CompaniesHouseResponseTransformer to modify payloads:
$transformer = new CustomResponseTransformer();
$service->setTransformer($transformer);
Mocking for Tests
Use the CompaniesHouseMockClient in PHPUnit:
$mockClient = new CompaniesHouseMockClient();
$service = new CompaniesHouseService($mockClient);
$service->getCompanyProfile('12345678'); // Returns mocked data
https://api.company-information.service.gov.uk. Override in config/packages/companies_house.yaml:
companies_house:
base_uri: 'https://api.test.company-information.service.gov.uk'
companies_house:
timeout: 30 # seconds
How can I help you explore Laravel packages today?