From f8f770f8d77455b728e2fd760503dc8ba2d315bc Mon Sep 17 00:00:00 2001 From: Lars Dieckow Date: Thu, 28 Mar 2013 17:23:01 +0100 Subject: [PATCH] dependency injection --- lib/NGCP/Schema.pm | 2 +- lib/NGCP/Schema/billing.pm | 11 ++++++++--- lib/NGCP/Schema/provisioning.pm | 10 ++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/NGCP/Schema.pm b/lib/NGCP/Schema.pm index 6a40e158..1a07d5e7 100644 --- a/lib/NGCP/Schema.pm +++ b/lib/NGCP/Schema.pm @@ -7,7 +7,7 @@ use Regexp::IPv6 qw($IPv6_re); our $VERSION = '1.000'; -has('config', is => 'ro', lazy => 1, default => sub { NGCP::Schema::Config->new->config }); +has('config', is => 'rw', lazy => 1, default => sub { NGCP::Schema::Config->new->config }); method validate($data, $mandatory_params, $optional_params?) { Exception->throw({ diff --git a/lib/NGCP/Schema/billing.pm b/lib/NGCP/Schema/billing.pm index c4d8a57b..e7bf2377 100644 --- a/lib/NGCP/Schema/billing.pm +++ b/lib/NGCP/Schema/billing.pm @@ -17,6 +17,11 @@ use NGCP::Schema qw(); use NGCP::Schema::provisioning qw(); use aliased 'NGCP::Schema::Exception'; +has('validator', is => 'rw', isa => 'NGCP::Schema', lazy => 1, default => sub { return NGCP::Schema->new; }); +has('provisioning', is => 'rw', isa => 'NGCP::Schema::provisioning', lazy => 1, default => sub { + return NGCP::Schema::provisioning->connect; +}); + method get_domain($reseller_id, $domain) { my %return; $return{domain} = $domain; @@ -86,7 +91,7 @@ method create_domain($data, $reseller_id?) { Exception->throw({ description => 'Client.Syntax.MalformedDomain', message => "malformed domain '$data->{domain}' in request", - }) unless NGCP::Schema->new->check_domain({domain => $data->{domain}}); + }) unless $self->validator->check_domain({domain => $data->{domain}}); # /FIXME $self->txn_do(λ{ # just to verify the domain does not exist for the reseller @@ -124,7 +129,7 @@ method create_domain($data, $reseller_id?) { }) if defined $reseller_id; } # domain may already exist in provisioning DB if another reseller uses it - my $provisioning = NGCP::Schema::provisioning->connect; + my $provisioning = $self->provisioning; if ($provisioning->get_domain({domain => $data->{domain}})) { $provisioning->update_domain($data); } else { @@ -142,7 +147,7 @@ method delete_domain($data, $reseller_id) { Exception->throw({ description => 'Client.Syntax.MalformedDomain', message => "malformed domain '$data->{domain}' in request", - }) unless NGCP::Schema->new->check_domain({domain => $data->{domain}}); + }) unless $self->validator->check_domain({domain => $data->{domain}}); $self->txn_do(λ{ my $remaining_resellers = $self->delete_domain($reseller_id, $data->{domain}); unless ($remaining_resellers) { diff --git a/lib/NGCP/Schema/provisioning.pm b/lib/NGCP/Schema/provisioning.pm index 38c13fd0..0a87a783 100644 --- a/lib/NGCP/Schema/provisioning.pm +++ b/lib/NGCP/Schema/provisioning.pm @@ -16,6 +16,8 @@ __PACKAGE__->load_namespaces; use NGCP::Schema qw(); use aliased 'NGCP::Schema::Exception'; +has('validator', is => 'rw', isa => 'NGCP::Schema', lazy => 1, default => sub { return NGCP::Schema->new; }); + method _get_domain_id($domain) { my $domainid = $self->resultset('voip_domains')->search({domain => $domain})->first->id; Exception->throw({ @@ -35,7 +37,7 @@ method create_domain($data) { Exception->throw({ description => 'Client.Syntax.MalformedDomain', message => "malformed domain '$data->{domain}' in request", - }) unless NGCP::Schema->new->check_domain({domain => $data->{domain}}); + }) unless $self->validator->check_domain({domain => $data->{domain}}); # /FIXME $self->txn_do(λ{ @@ -62,7 +64,7 @@ method get_domain($data) { Exception->throw({ description => 'Client.Syntax.MalformedDomain', message => "malformed domain '$data->{domain}' in request", - }) unless NGCP::Schema->new->check_domain({domain => $data->{domain}}); + }) unless $self->validator->check_domain({domain => $data->{domain}}); return $self->resultset('voip_domains')->search({id => $self->_get_domain_id($data->{domain})}); } @@ -74,7 +76,7 @@ method update_domain($data) { Exception->throw({ description => 'Client.Syntax.MalformedDomain', message => "malformed domain '$data->{domain}' in request", - }) unless NGCP::Schema->new->check_domain({domain => $data->{domain}}); + }) unless $self->validator->check_domain({domain => $data->{domain}}); $self->txn_do(λ{ my $domainid = $self->_get_domain_id($data->{domain}); }); @@ -90,7 +92,7 @@ method delete_domain($data) { Exception->throw({ description => 'Client.Syntax.MalformedDomain', message => "malformed domain '$data->{domain}' in request", - }) unless NGCP::Schema->new->check_domain({domain => $data->{domain}}); + }) unless $self->validator->check_domain({domain => $data->{domain}}); $self->txn_do(λ{ my $domainid = $self->_get_domain_id($data->{domain}); $self->resultset('voip_domains')->search(